Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数组的if语句_Python_Arrays_Python 3.x_If Statement - Fatal编程技术网

Python 数组的if语句

Python 数组的if语句,python,arrays,python-3.x,if-statement,Python,Arrays,Python 3.x,If Statement,如何使二维数组成为if语句 for row in range(6): for column in range(7): if board[row][column] > 0: Draw = True 我有一个由7乘6的空白正方形组成的网格,如果单击一个正方形,它的给定值为1,否则它将保持为0。当所有的方块都被点击时,使now 1 I want Draw=True,但是上面的语句会找到我刚才选择的行和列,而不是整个网格。如果你的意思是检查整个电路

如何使二维数组成为if语句

for row in range(6):
    for column in range(7):
        if board[row][column] > 0:
            Draw = True

我有一个由7乘6的空白正方形组成的网格,如果单击一个正方形,它的给定值为1,否则它将保持为0。当所有的方块都被点击时,使now 1 I want Draw=True,但是上面的语句会找到我刚才选择的行和列,而不是整个网格。

如果你的意思是检查整个电路板是否都是1,那么这是一个简单的修复

DRAW = True

for row in range(6):
    for column in range(7):
        if board[row][column] == 0:
            DRAW = False

用另一种方法更容易。

您需要的是,只有当所有
board
值都为1时,
Draw
才设置为True。如果单击了任何网格,则代码将
Draw
设置为1

解决方案很简单,您可以反向思考,将
绘图设置为True,每当网格不是1时,将其设置为False

Draw = True
for row in range(6):
    for column in range(7):
        if board[row][column] == 0:
            Draw = False

如果需要检查集合中的所有值是否具有
True
的值,则始终可以使用内置函数检查iterable中的所有值是否满足给定条件(在您的情况下为
i>0
):

可以简化为:

if all(i for i in board[row] for row in range(6)): 
    Draw = True

由于正值的计算结果为
True

,因此可以使用
all
作为@Wboy的forloop的另一种方法:

all(x for y in z for x in y)

#or x !=0 but that's redundant here since 1 equates to True.
这里,z是你的二维列表。您也可以通过这种方式评估不均匀尺寸,而不会“陷入”范围等

警告:如果您的网格中有一个空列表,那么这里的
all
仍将计算为true

警告的示例网格:

[[],
 [1, 1, 1],
 [1, 1], #some more rand elements of the grid, first one's the point.
]

需要更多的澄清。你的意思是当整个棋盘为“1”时,然后设置draw=True?是的,这就是我的意思,对不起,是的,更好的方法:)
[[],
 [1, 1, 1],
 [1, 1], #some more rand elements of the grid, first one's the point.
]