【问题标题】:I want to make a hour glass out of the 2D array我想用二维数组制作一个沙漏
【发布时间】: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

我想将所有可能的沙漏制作成这个 2D 列表,如下所示:

并打印每个沙漏的总和

注意:我使用的是python3

【问题讨论】:

  • 到目前为止你有什么尝试?

标签: arrays python-3.x sub-array


【解决方案1】:
#! /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()

【讨论】:

  • 我编辑了问题,你能再看看吗?
猜你喜欢
  • 2016-10-27
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-17
  • 2021-12-31
相关资源
最近更新 更多