【问题标题】:Trying to loop through a two dimentional array in python试图在python中循环一个二维数组
【发布时间】:2021-07-18 12:04:34
【问题描述】:

我试图通过循环来查找二维数组中的块,但我的 _x 变量始终保持为零,我该怎么办?如果需要,我可以提供更多信息

_y=0
_x=0
while _x<= len(blocks_list)-1:
    while _y <= len(blocks_list[_x])-1:
        if blocks_list[_x][_y] != -1:
            pygame.draw.rect(screen,white,pygame.Rect(_x*block_size,_y*block_size,block_size,block_size))
        _y+=1
    _x+=1

【问题讨论】:

  • blocks_listblock_size 是什么?

标签: python list arraylist


【解决方案1】:
_x=0
while _x < len(blocks_list)-1:
    _y=0
    while _y < len(blocks_list[_x])-1:
        if blocks_list[_x][_y] != -1:
            // do your stuff
            print(_x, _y)
        _y+=1
    _x+=1

【讨论】:

  • 非常感谢!我现在因为没有意识到这一点而感到愚蠢。
  • 移动 _y=0 是正确的,但我不确定 _y+=1_x+=1 现在是否正确缩进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多