I am trying to have a program look inside multiple lists to see if there are multiple integers in those lists. I received an answer on a previous question I asked to accomplish that task. However a problem arose with that answer and I can not figure out what it is.
我正在尝试让一个程序查看多个列表,看看这些列表中是否有多个整数。为了完成这项任务,我曾问过一个问题。然而,这个答案产生了一个问题,我想不出它是什么。
Here is the code:
这是代码:
def all_num_in_list(d, numbers):
for n in numbers:
if n not in d:
return False
return True
def all_num_in_any_list(lists, numbers):
for d in lists:
if all_num_in_list(d, numbers):
return True
return False
while a:
try:
for c, row in enumerate(matrix):
if 0 in row:
print("Found 0 on row:", c, "index:", row.index(0))
if 1 not in row:
if all(row[row.index(0)] != 1 for row in matrix):
if all_num_in_any_list([2, 3, 4, 5, 6, 7, 8, 9], [row, box1, all(row[row.index(0)])]):
if all(row[row.index(0)] != 1 for row in matrix):
print ("t")
The error that it draws is:
它绘制的错误是:
if all_num_in_any_list([2, 3, 4, 5, 6, 7, 8, 9], [row, box1, all(row[row.index(0)])]):
TypeError: 'int' object is not iterable
Why is this happening, how can it be avoided, and what exactly is this code doing?
为什么会发生这种情况,如何避免这种情况,代码到底在做什么?
Thanks
谢谢
4 个解决方案
#1
2
The first parameter to all_num_in_any_list, [2,3,4,5,6,7,8,9], is a single list, not a list of lists. When you iterate through it, d is 1, then d is 2, and so forth. When you pass d as the first parameter to all_num_in_list, it is trying to treat it as a list even though it is an integer.
all_num_in_any_list的第一个参数是一个列表,而不是一个列表列表。当你迭代它时,d是1,然后d是2,以此类推。当您将d作为第一个参数传递给all_num_in_list时,它试图将它作为一个列表来处理,即使它是一个整数。
#2
2
Here is your matrix, from an earlier question
这是你的矩阵,从之前的一个问题。
matrix = [
[0, 0, 0, 5, 0, 0, 0, 0, 6],
[8, 0, 0, 0, 4, 7, 5, 0, 3],
[0, 5, 0, 0, 0, 3, 0, 0, 0],
[0, 7, 0, 8, 0, 0, 0, 0, 9],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[9, 0, 0, 0, 0, 4, 0, 2, 0],
[0, 0, 0, 9, 0, 0, 0, 1, 0],
[7, 0, 8, 3, 2, 0, 0, 0, 5],
[3, 0, 0, 0, 0, 8, 0, 0, 0],
]
And here is the methods:
这里有一些方法:
def all_num_in_list(d, numbers):
for n in numbers:
if n not in d:
return False
return True
def all_num_in_any_list(lists, numbers):
for d in lists:
if all_num_in_list(d, numbers):
return True
return False
And since you in an earlier question used the numbers, 3,5 and 6 as examples to look at, here is how you check if these numbers are in the matrix above:
由于你在之前的问题中使用了数字3 5 6作为例子,这是你如何检查这些数字是否在上面的矩阵中:
all_num_in_any_list(matrix, [3, 5, 6])
Which will return False
, as none of the lists in your list of lists will have all these tree numbers, while for example:
返回False,因为列表中的列表中没有一个列表包含所有这些树号,例如:
all_num_in_any_list(matrix, [0, 1, 9])
will return True, as there is a list that includes these numbers.
将返回True,因为有一个包含这些数字的列表。
#3
0
all_num_in_any_list extacts elements from lists, which in this case are all integers; then, it passes each integer to all_num_in_list, which is expecting a list instead.
all_num_in_any_list扩展列表中的元素,在本例中这些元素都是整数;然后,它将每个整数传递给all_num_in_list,后者期望得到一个列表。
#4
0
The statement for n in numbers
in this code is causing the problem
这个代码中的n的语句导致了这个问题。
def all_num_in_list(d, numbers):
for n in numbers:
if n not in d:
return False
return True
The Partial Traceback is
部分回溯是
if n not in d
def all_num_in_list(d, numbers):
def all_num_in_any_list(lists, numbers):
if all_num_in_any_list([2, 3, 4, 5, 6, 7, 8, 9], [row, box1, all(row[row.index(0)])]):
TypeError: 'int' object is not iterable
So in function all_num_in_any_list
you are already iterating for d in lists
over the list lists
to get integers which is passed to all_num_in_list
. But here while trying to compare n
with an atom d
, is where the Error Happened.
因此,在函数all_num_in_any_list中,您已经在列表列表中迭代了d,以获得传递到all_num_in_list的整数。但是在这里,当试图比较n和一个原子d时,错误发生了。
Think Over?
想结束了吗?
Did you intend to do an integer comparition like n != d
instead of if n not in d
.
你想做一个像n != d这样的整数比较而不是如果n不在d中。
Note::: Next Time please post the Complete Traceback
注:::下次请张贴完整的回溯
#1
2
The first parameter to all_num_in_any_list, [2,3,4,5,6,7,8,9], is a single list, not a list of lists. When you iterate through it, d is 1, then d is 2, and so forth. When you pass d as the first parameter to all_num_in_list, it is trying to treat it as a list even though it is an integer.
all_num_in_any_list的第一个参数是一个列表,而不是一个列表列表。当你迭代它时,d是1,然后d是2,以此类推。当您将d作为第一个参数传递给all_num_in_list时,它试图将它作为一个列表来处理,即使它是一个整数。
#2
2
Here is your matrix, from an earlier question
这是你的矩阵,从之前的一个问题。
matrix = [
[0, 0, 0, 5, 0, 0, 0, 0, 6],
[8, 0, 0, 0, 4, 7, 5, 0, 3],
[0, 5, 0, 0, 0, 3, 0, 0, 0],
[0, 7, 0, 8, 0, 0, 0, 0, 9],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[9, 0, 0, 0, 0, 4, 0, 2, 0],
[0, 0, 0, 9, 0, 0, 0, 1, 0],
[7, 0, 8, 3, 2, 0, 0, 0, 5],
[3, 0, 0, 0, 0, 8, 0, 0, 0],
]
And here is the methods:
这里有一些方法:
def all_num_in_list(d, numbers):
for n in numbers:
if n not in d:
return False
return True
def all_num_in_any_list(lists, numbers):
for d in lists:
if all_num_in_list(d, numbers):
return True
return False
And since you in an earlier question used the numbers, 3,5 and 6 as examples to look at, here is how you check if these numbers are in the matrix above:
由于你在之前的问题中使用了数字3 5 6作为例子,这是你如何检查这些数字是否在上面的矩阵中:
all_num_in_any_list(matrix, [3, 5, 6])
Which will return False
, as none of the lists in your list of lists will have all these tree numbers, while for example:
返回False,因为列表中的列表中没有一个列表包含所有这些树号,例如:
all_num_in_any_list(matrix, [0, 1, 9])
will return True, as there is a list that includes these numbers.
将返回True,因为有一个包含这些数字的列表。
#3
0
all_num_in_any_list extacts elements from lists, which in this case are all integers; then, it passes each integer to all_num_in_list, which is expecting a list instead.
all_num_in_any_list扩展列表中的元素,在本例中这些元素都是整数;然后,它将每个整数传递给all_num_in_list,后者期望得到一个列表。
#4
0
The statement for n in numbers
in this code is causing the problem
这个代码中的n的语句导致了这个问题。
def all_num_in_list(d, numbers):
for n in numbers:
if n not in d:
return False
return True
The Partial Traceback is
部分回溯是
if n not in d
def all_num_in_list(d, numbers):
def all_num_in_any_list(lists, numbers):
if all_num_in_any_list([2, 3, 4, 5, 6, 7, 8, 9], [row, box1, all(row[row.index(0)])]):
TypeError: 'int' object is not iterable
So in function all_num_in_any_list
you are already iterating for d in lists
over the list lists
to get integers which is passed to all_num_in_list
. But here while trying to compare n
with an atom d
, is where the Error Happened.
因此,在函数all_num_in_any_list中,您已经在列表列表中迭代了d,以获得传递到all_num_in_list的整数。但是在这里,当试图比较n和一个原子d时,错误发生了。
Think Over?
想结束了吗?
Did you intend to do an integer comparition like n != d
instead of if n not in d
.
你想做一个像n != d这样的整数比较而不是如果n不在d中。
Note::: Next Time please post the Complete Traceback
注:::下次请张贴完整的回溯