检查值是否在列表列表中总是 returns False
Checking if a value is in a list of lists always returns False
目前正在研究一个小型数独求解器,我使用布尔值
if 0 in BOARD_EASY:
do the thing
出于测试目的将板作为此值:
BOARD_EASY =[
[7,2,5,8,1,3,6,4,9],
[4,8,6,9,2,7,1,5,3],
[3,1,9,6,5,4,8,7,2],
[5,6,4,2,7,1,3,9,8],
[8,9,1,5,3,9,4,2,7],
[2,7,3,4,8,9,5,6,1],
[6,3,8,7,9,5,2,1,4],
[9,4,2,1,6,8,7,3,5],
[0,0,7,3,4,2,9,8,6]
]
由于某些原因,布尔值 return 为假,而棋盘上仍然有 0,我是否遗漏了什么?
它似乎不像 board_easy 的第一行那样检查(例如,我尝试删除最后一位数字)
编辑:允许我完成我的项目,谢谢大家! (https://github.com/Lem0nRavioli/sudoku_solver)
如果您只检查索引之一中元素的成员资格,则不需要嵌套 for 循环:
for count, i in enumerate(BOARD_EASY):
if 0 in i:
print("Found it in index", count)
Board_easy 是列表的列表,因此第一个元素是 [7,2,5...] 而不是 7。您将需要一个嵌套的 for 循环来遍历每个维度
BOARD_EASY
是列表的列表。 BOARD_EASY
不包含 0
...它包含 9 个列表。
看这里:
BOARD_EASY = [
[7, 2, 5, 8, 1, 3, 6, 4, 9],
[4, 8, 6, 9, 2, 7, 1, 5, 3],
[3, 1, 9, 6, 5, 4, 8, 7, 2],
[5, 6, 4, 2, 7, 1, 3, 9, 8],
[8, 9, 1, 5, 3, 9, 4, 2, 7],
[2, 7, 3, 4, 8, 9, 5, 6, 1],
[6, 3, 8, 7, 9, 5, 2, 1, 4],
[9, 4, 2, 1, 6, 8, 7, 3, 5],
[0, 0, 7, 3, 4, 2, 9, 8, 6]
]
for i, x in enumerate(BOARD_EASY):
print(i+1, x)
结果:
1 [7, 2, 5, 8, 1, 3, 6, 4, 9]
2 [4, 8, 6, 9, 2, 7, 1, 5, 3]
3 [3, 1, 9, 6, 5, 4, 8, 7, 2]
4 [5, 6, 4, 2, 7, 1, 3, 9, 8]
5 [8, 9, 1, 5, 3, 9, 4, 2, 7]
6 [2, 7, 3, 4, 8, 9, 5, 6, 1]
7 [6, 3, 8, 7, 9, 5, 2, 1, 4]
8 [9, 4, 2, 1, 6, 8, 7, 3, 5]
9 [0, 0, 7, 3, 4, 2, 9, 8, 6]
您要查找的等效代码按顺序检查每个列表:
for i, l in enumerate(BOARD_EASY):
if 0 in l:
print("There's a zero in row " + str(i + 1))
结果:
There's a zero in row 9
我相信 in
运算符不检查嵌套值,只检查第一层。
所以它正在查看您的列表,并且只看到其他列表。见下文。
BOARD_EASY =[
[7,2,5,8,1,3,6,4,9], #-> list
[4,8,6,9,2,7,1,5,3], #-> list
[3,1,9,6,5,4,8,7,2], #-> list
[5,6,4,2,7,1,3,9,8], #-> list
[8,9,1,5,3,9,4,2,7], #-> list
[2,7,3,4,8,9,5,6,1], #-> list
[6,3,8,7,9,5,2,1,4], #-> list
[9,4,2,1,6,8,7,3,5], #-> list
[0,0,7,3,4,2,9,8,6] #-> list
]
因此正确的解决方案是循环遍历列表列表 BOARD_EASY
然后循环遍历每个列表的内容:
for nested_list in BOARD_EASY:
for items in nested_list:
if 0 in items:
print("Found!")
根据 in
运算符的文档:
The operators in
and not in
test for membership. x in s
evaluates to True
if x is a member of s, and False
otherwise.
0
不是 BOARD_EASY
的成员。列表 [0,0,7,3,4,2,9,8,6]
是。
所以你可以这样做:
any(0 in elem for elem in BOARD_EASY)
你可以使用 numpy.flatten:
BOARD_EASY = np.array(BOARD_EASY).flatten()
print("do the thing") if 0 in BOARD_EASY else False
do the thing
我看到所有的答案都是遍历所有元素,或者复制所有数据,或者实现 3 行代码来完成,所以这里有一个快速简单的解决方案:
if 0 in itertools.chain(*BOARD_EASY):
do the thing
chain
的作用是遍历 BOARD_EASY
中的每一项,就好像它只是遍历一个列表一样。
感谢所有的回答,让我明白了 'in' 运算符是怎么回事。
最后使用了与 David Erickson 之一相关的一行:
# start of the script
BOARD_EASY = np.array(BOARD_EASY)
#
#
if 0 in np.concatenate(BOARD_EASY):
do the thing
我知道您对此已经有了回应,我想分享一个使用 any
最小化迭代的选项
if any(0 in elem for elem in BOARD_EASY):
print ('0 is found in BOARD_EASY')
else:
print ('0 is NOT found in BOARD_EASY')
any
函数的重要性在于它short-circuits一旦找到值就执行。所以它不会遍历整个列表。
查看您的代码,您的 if 语句 if 0 in BOARD_EASY:
将导致 if (0 == BOARD_EASY[0]) or (0 == BOARD_EASY[1]) .....
。该列表有很多元素,因此比较不正确。您需要查找列表中的每个项目并比较内部列表中的元素。使用上面的代码(使用任何函数),它就可以了。
目前正在研究一个小型数独求解器,我使用布尔值
if 0 in BOARD_EASY:
do the thing
出于测试目的将板作为此值:
BOARD_EASY =[
[7,2,5,8,1,3,6,4,9],
[4,8,6,9,2,7,1,5,3],
[3,1,9,6,5,4,8,7,2],
[5,6,4,2,7,1,3,9,8],
[8,9,1,5,3,9,4,2,7],
[2,7,3,4,8,9,5,6,1],
[6,3,8,7,9,5,2,1,4],
[9,4,2,1,6,8,7,3,5],
[0,0,7,3,4,2,9,8,6]
]
由于某些原因,布尔值 return 为假,而棋盘上仍然有 0,我是否遗漏了什么? 它似乎不像 board_easy 的第一行那样检查(例如,我尝试删除最后一位数字)
编辑:允许我完成我的项目,谢谢大家! (https://github.com/Lem0nRavioli/sudoku_solver)
如果您只检查索引之一中元素的成员资格,则不需要嵌套 for 循环:
for count, i in enumerate(BOARD_EASY):
if 0 in i:
print("Found it in index", count)
Board_easy 是列表的列表,因此第一个元素是 [7,2,5...] 而不是 7。您将需要一个嵌套的 for 循环来遍历每个维度
BOARD_EASY
是列表的列表。 BOARD_EASY
不包含 0
...它包含 9 个列表。
看这里:
BOARD_EASY = [
[7, 2, 5, 8, 1, 3, 6, 4, 9],
[4, 8, 6, 9, 2, 7, 1, 5, 3],
[3, 1, 9, 6, 5, 4, 8, 7, 2],
[5, 6, 4, 2, 7, 1, 3, 9, 8],
[8, 9, 1, 5, 3, 9, 4, 2, 7],
[2, 7, 3, 4, 8, 9, 5, 6, 1],
[6, 3, 8, 7, 9, 5, 2, 1, 4],
[9, 4, 2, 1, 6, 8, 7, 3, 5],
[0, 0, 7, 3, 4, 2, 9, 8, 6]
]
for i, x in enumerate(BOARD_EASY):
print(i+1, x)
结果:
1 [7, 2, 5, 8, 1, 3, 6, 4, 9]
2 [4, 8, 6, 9, 2, 7, 1, 5, 3]
3 [3, 1, 9, 6, 5, 4, 8, 7, 2]
4 [5, 6, 4, 2, 7, 1, 3, 9, 8]
5 [8, 9, 1, 5, 3, 9, 4, 2, 7]
6 [2, 7, 3, 4, 8, 9, 5, 6, 1]
7 [6, 3, 8, 7, 9, 5, 2, 1, 4]
8 [9, 4, 2, 1, 6, 8, 7, 3, 5]
9 [0, 0, 7, 3, 4, 2, 9, 8, 6]
您要查找的等效代码按顺序检查每个列表:
for i, l in enumerate(BOARD_EASY):
if 0 in l:
print("There's a zero in row " + str(i + 1))
结果:
There's a zero in row 9
我相信 in
运算符不检查嵌套值,只检查第一层。
所以它正在查看您的列表,并且只看到其他列表。见下文。
BOARD_EASY =[
[7,2,5,8,1,3,6,4,9], #-> list
[4,8,6,9,2,7,1,5,3], #-> list
[3,1,9,6,5,4,8,7,2], #-> list
[5,6,4,2,7,1,3,9,8], #-> list
[8,9,1,5,3,9,4,2,7], #-> list
[2,7,3,4,8,9,5,6,1], #-> list
[6,3,8,7,9,5,2,1,4], #-> list
[9,4,2,1,6,8,7,3,5], #-> list
[0,0,7,3,4,2,9,8,6] #-> list
]
因此正确的解决方案是循环遍历列表列表 BOARD_EASY
然后循环遍历每个列表的内容:
for nested_list in BOARD_EASY:
for items in nested_list:
if 0 in items:
print("Found!")
根据 in
运算符的文档:
The operators
in
andnot in
test for membership.x in s
evaluates toTrue
if x is a member of s, andFalse
otherwise.
0
不是 BOARD_EASY
的成员。列表 [0,0,7,3,4,2,9,8,6]
是。
所以你可以这样做:
any(0 in elem for elem in BOARD_EASY)
你可以使用 numpy.flatten:
BOARD_EASY = np.array(BOARD_EASY).flatten()
print("do the thing") if 0 in BOARD_EASY else False
do the thing
我看到所有的答案都是遍历所有元素,或者复制所有数据,或者实现 3 行代码来完成,所以这里有一个快速简单的解决方案:
if 0 in itertools.chain(*BOARD_EASY):
do the thing
chain
的作用是遍历 BOARD_EASY
中的每一项,就好像它只是遍历一个列表一样。
感谢所有的回答,让我明白了 'in' 运算符是怎么回事。 最后使用了与 David Erickson 之一相关的一行:
# start of the script
BOARD_EASY = np.array(BOARD_EASY)
#
#
if 0 in np.concatenate(BOARD_EASY):
do the thing
我知道您对此已经有了回应,我想分享一个使用 any
if any(0 in elem for elem in BOARD_EASY):
print ('0 is found in BOARD_EASY')
else:
print ('0 is NOT found in BOARD_EASY')
any
函数的重要性在于它short-circuits一旦找到值就执行。所以它不会遍历整个列表。
查看您的代码,您的 if 语句 if 0 in BOARD_EASY:
将导致 if (0 == BOARD_EASY[0]) or (0 == BOARD_EASY[1]) .....
。该列表有很多元素,因此比较不正确。您需要查找列表中的每个项目并比较内部列表中的元素。使用上面的代码(使用任何函数),它就可以了。