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
The statement for n in numbers
in this code is causing the problem
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.
Think Over?
Did you intend to do an integer comparition like n != d
instead of if n not in d
.
Note::: Next Time please post the Complete Traceback
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.
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.
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:
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:
all_num_in_any_list(matrix, [0, 1, 9])
will return True, as there is a list that includes these numbers.