【发布时间】:2021-06-27 06:11:21
【问题描述】:
我正在尝试形成这个二维数组的沙漏
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
并打印每个沙漏的总和
注意:我使用的是python3
【问题讨论】:
-
到目前为止你有什么尝试?
标签: arrays python-3.x sub-array
我正在尝试形成这个二维数组的沙漏
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
并打印每个沙漏的总和
注意:我使用的是python3
【问题讨论】:
标签: arrays python-3.x sub-array
#! /usr/bin/env python3
import turtle as trt
import numpy as np
import math
Width, Height = 700, 700
trt ._CFG['width'], trt ._CFG['height'] = Width *1.02, Height *1.02 ## no scrollbars
trt ._CFG['canvwidth'], trt ._CFG['canvheight'] = Width, Height ## no external turtle.cfg required
turtle, screen = trt .Turtle(), trt .Screen()
screen .setworldcoordinates( 0, Height, Width, 0 )
screen .title( 'Hourglass' )
screen .tracer( 42, 0 ) ## nth screen draw, delay
scale = math .sqrt( Width *Width + Height *Height ) *0.5
padding = ( max( Width, Height ) -scale )
screen .colormode( 1 ) ## range 0-1, defaults 0-255
array = np .array( [ [ 1, 1, 1, 0, 0, 0 ],
[ 0, 1, 0, 0, 0, 0 ],
[ 1, 1, 1, 0, 0, 0 ],
[ 0, 0, 2, 4, 4, 0 ],
[ 0, 0, 0, 2, 0, 0 ],
[ 0, 0, 1, 2, 4, 0 ] ] )
turtle .penup()
turtle .shapesize( 4 )
turtle .shape( 'circle' )
for y in range( len( array ) ):
for x in range( len( array[y] ) ):
current_pos = array[y][x]
if current_pos > 0:
if current_pos > 2: turtle. color( 'red' )
elif current_pos > 1: turtle. color( 'blue' )
else: turtle. color( 'black' )
turtle .goto( x *scale /6 +padding, y *scale /6 +padding /2 )
turtle .stamp()
screen .update() ## for tracer
screen .listen() ## for mouse & key presses
screen .exitonclick() ## binds .bye() to screen click
screen .mainloop()
【讨论】: