TypeError: 'int' object is not iterable

前端 未结 4 425
[愿得一人]
[愿得一人] 2021-01-29 16:21

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

4条回答
  •  日久生厌
    2021-01-29 16:51

    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.

提交回复
热议问题