当且仅当所有给定条件均为tru时

2024-10-11 16:24:13 发布

您现在位置:Python中文网/ 问答频道 /正文

import numpy as np    
def data_verify(source):
        rows = [x.strip().split(' ') for x in open(source)]
        columns = zip(*rows)
        blocks = np.array(rows).reshape((3,3,3,3)).transpose((0,2,1,3)).reshape((9,9))
            #check iff, see further
                return rows, columns, blocks
            else:
                return False

比如数独游戏:

^{pr2}$

该函数收集所有相关数据,并在行的长度与列的长度相同的情况下返回相应的行、列和块(获得了一些其他函数来确定谜题是否合法)。我认为将第一行与所有列进行比较就足够了(反之亦然,也没有什么区别)。我如何才能创建一张支票,它的内容如下:

for i in range(len(rows)):
    if len(row[0]) == len(column[i]):
        #do something only if all of the lengths check out

Tags: columns函数inimportnumpysourceforlen
2条回答

使用^{}

if all(len(row[0]) == len(column[i]) for i in range(len(rows))):
    #do something only if all of the lengths check out

您可以在for循环中运行check并设置一个标志如果有任何不匹配,此示例将检查包含所有列的所有行:

match = True
for r in row:
    for c in column:
        if len(c) != len(r):
            match = False

# Only continue if match == True

相关问题 更多 >

    热门问题