-4

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

chingchong
  • 493
  • 2
  • 8
  • 11
  • You have an integer, while you expect otherwise. So which value (variable) is it? –  Jan 04 '12 at 03:22
  • hisss!! that's some nasty nested python code – wim Jan 04 '12 at 03:54
  • -1 You should have included the traceback and more of the code. You have asked so many questions now, you should know this. – Lennart Regebro Jan 04 '12 at 08:14
  • Can you provide a couple of small examples, each with a matrix & what its output should be? – Scott Hunter Jan 06 '12 at 20:00
  • Does this answer your question? [How do I fix TypeError: 'int' object is not iterable?](https://stackoverflow.com/questions/14941288/how-do-i-fix-typeerror-int-object-is-not-iterable) – Thomas Weller Nov 02 '22 at 08:14

4 Answers4

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.

Jim
  • 641
  • 4
  • 9
  • The two functions all_num_in_list and all_num_in_any_list are both correct, but the 'while' loop is probably quite broken. It is not clear what it is attempting to do. As far as all_num_in_any_list, the list of numbers is the second parameter, the list of lists is the first parameter. The following will work: all_num_in_any_list([[1,2,3],[4,5,6]],[1,3]) – Jim Jan 04 '12 at 04:08
  • It is supposed to be searching through those lists: row, box1, and all(row[row.index(0)] to find the numbers 2 - 9. The numbers could be found anywhere throughout any of the lists and don't have to all be in one list. So if say 3, 5, 7, were in one and 2, 4, 6 were in another and 8, 9 were in the last one it would work. – chingchong Jan 04 '12 at 16:22
  • @chingchong: all_num_in_any_lists takes two parameters. The first parameter, lists, should be is a list of lists, and the second. numbers, should be a list of numbers. You pass in the list of numbers as the first parameter, and the list of lists as the second. – Lennart Regebro Jan 04 '12 at 16:52
  • Why does the first have to be a list of lists? I do not want to search a list of lists. I want to search a list, that is what I asked for in my first question. – chingchong Jan 04 '12 at 17:23
  • If you look at the title of that question it is "Search through multiple lists" Not search through list of lists and lists. – chingchong Jan 04 '12 at 17:25
  • @chingchong: A list of lists *is* multiple lists. Same thing. – Lennart Regebro Jan 04 '12 at 17:37
  • Yes a list of lists is multiple lists but multiple lists are not necessarily a list of lists. I did not say I wanted to search through a list of lists that is easy. What I want to do is, as I have said time and time again, search through multiple lists (NOT IN A LIST OF LISTS!!!!!!) to see if the numbers are all there spread out through the lists. – chingchong Jan 04 '12 at 18:42
  • @chingchong: If you want to search through multiple lists, pass in those multiple lists to the function, in the form of a list. Ie a list of lists. Multiple lists == list of lists. OK? – Lennart Regebro Jan 04 '12 at 23:31
  • So could you please tell me how to properly search through these three lists? – chingchong Jan 05 '12 at 22:49
  • @chingchong: I have already told you that, over and over and over. I don't think telling you again is going to make a difference. – Lennart Regebro Jan 06 '12 at 10:17
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:

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.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • Well I already knew all of the information you gave me here. I have no idea why you are looking at earlier questions in which I ask about totally different things but you did. And it didn't help me at all. The only thing I want is to know how to fix your code so that it will not pull an error. i don't need you to copy and paste old questions and answers because I obviously already know them. If you could just answer my question I would be extremely happy. Thanks. – chingchong Jan 04 '12 at 16:11
  • I give up. Your earlier self-assessment seems completely accurate. – Lennart Regebro Jan 04 '12 at 16:55
  • I could have accomplished this before. I do not want to search "matrix". That is not what I am trying to do! I want to search through multiple lists! I think I made this quite clear to you. Several times in our conversation, and even in your own answer, you said it did what I wanted it to do. But now your telling me I'm supposed to be searching matrix!! This is outrageous. You act like you are the immanuel and have done everything so well but you just lied to me! – chingchong Jan 04 '12 at 17:30
  • So you are not supposed to search the matrix? That's what you said you wanted: "I am trying to have a program look inside multiple lists" That's what you said. Well, a list of lists is a matrix. Same thing. I have not lied to you, the problem is that you, as you so correctly pointed out, are a moron. – Lennart Regebro Jan 04 '12 at 17:35
  • MULTIPLE LISTS!!! Oh my god!!! You quoted me saying "multiple lists" then you say "a list of lists is a matrix" Did I, once, say list of lists!! NO I did not!! I don't appreciate you putting words into my mouth. I DO NOT WANT A LIST OF LISTS!!! Is that clear to you??!! Because I don't have the ability to make it any clearer. repeat: I DONT WANT TO SEARCH A LIST OF LISTS!!!!!!!!!!!!!!!!!!! – chingchong Jan 04 '12 at 18:51
  • @chingchong: Once again: Multiple lists and a list of lists is for all intents and purposes the exact same thing. Your "multiple lists" `[row, box1, all(row[row.index(0)])]` is a list of lists. Also known as a matrix. It's all the same thing in this context. – Lennart Regebro Jan 04 '12 at 23:29
  • But I don't want to search a list of lists. like matrix!!!! I want to search through row, box1, and all(row[row.index(0)]) Although this is a lists of lists, listing these lists like this, it is different from searching a predefined list of lists. – chingchong Jan 05 '12 at 22:47
  • What I would like to know, if you would be so inclined to tell me, is how I could possibly accomplish this task. Instead of making fun of me for a few minutes, could you please tell me how to do what you know I want to do correctly? Thanks. All I need to know is how I can turn these three lists into a list of lists. – chingchong Jan 06 '12 at 06:05
  • 1
    @chingchong: Like so: `[list1, list2, list3]` That is, as you see a list. Containing lists. ie a list of lists. – Lennart Regebro Jan 06 '12 at 10:09
  • @chingchong: Your silence to this is telling. Once again I have to point out that not everybody have the sort of logical mind that lends itself to programming. Not everyone can learn it, quite simply. I suspect you should choose another career path, because as far as I can tell, you haven't understood one single thing anyone has told you in most of your questions. – Lennart Regebro Jan 06 '12 at 22:13
  • [row, box1, all(row[row.index(0)] = [list1, list2, list3] – chingchong Jan 07 '12 at 20:08
  • Yes, I have this list containing lists. So why doesn't it work!?! – chingchong Jan 07 '12 at 20:09
  • @chingchong: Yes, that's exactly what you have. A list of lists. Yet you told me repeatedly that you didn't want to search a list of lists. Why doesn't it work? Well, we've told you over and over and over. – Lennart Regebro Jan 07 '12 at 20:22
  • Use the isinstance function to make sure that what you are processing is a list, as follows: if isinstance(item, list). If item gets an integer, isinstance will return false and you will not try to iterate over it. @Lennart Regebro: What do you think? – Sabuncu Mar 03 '12 at 15:21
  • @Sabuncu: It is a list of lists, there is nothing to check. – Lennart Regebro Mar 03 '12 at 16:46
  • @LennartRegebro: At some point, he's passing an int where a list is expected (thus the error). The isinstance BIF would protect against that, I was thinking. – Sabuncu Mar 03 '12 at 16:49
  • @Sabuncu: No, he should stop passing the int. It's unintentional, because he didn't really understand what he was doing. Hopefully he does now. :-) – Lennart Regebro Mar 03 '12 at 18:05
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.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • @chingchong: You can pass a list of lists, where you now pass a list, as you have been told by several people now. – Lennart Regebro Jan 04 '12 at 16:48
  • No one has told me that actually, must be one of your delusions. – chingchong Jan 04 '12 at 18:53
  • @chingchong: Both Jim and Abhijit told you that. But I guess you didn't understand what they said. And you didn't even understand that you didn't understand. – Lennart Regebro Jan 04 '12 at 23:34
  • The problem with that is that it is not at all what I want. They told me how your code was wrong. They said that an integer was present instead of a list. Just sticking in a list of lists won't help. I don't know why you think that if that is what you intended the code to do that it would fix everything. I don't want to search a list of lists I want to search through multiple lists though. The thing that would help me correctly use your code is if you told me a way to compile multiple lists into a list of lists. that would be something useful to me. but right now your code doesn't help me. – chingchong Jan 06 '12 at 03:21
  • @chingchong: Again, a list of lists *are* multiple lists. You *do* want to search through a list of lists. I've, Jim and Abhijit have already told you how to correctly use my code multiple times. You just don't understand the answers. – Lennart Regebro Jan 06 '12 at 10:17
  • No, it isn't that I don't understand your answers it is that I don't want to use a predefined list of lists. Good riddance, I may as well ask questions in an environment where it is possible to learn without being put down all of the time by a jerk like yourself. – chingchong Jan 06 '12 at 14:04
  • @chingchong: Who said "predefined"? I didn't say "predefined". – Lennart Regebro Jan 06 '12 at 22:11
  • Oh please, please almighty Lennart Regebro just tell me how to use your code successfully!!!! You now know that I want to search three lists just say how to do it. Thanks! – chingchong Jan 07 '12 at 18:50
  • @chingchong: I have told you. I spell it out exactly in my answer. – Lennart Regebro Jan 07 '12 at 21:11
0

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

Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • I did post the complete traceback. I do not know exactly what this code is supposed to be doing as the person who answered my last question with this code would not tell me. I am trying to learn Python so I don't actually know all of these things. The ultimate goal of this code was to make an if statement that ran if multiple numbers were found in any of three lists. They could be combined throughout so they don't all have to be from one list. Here is the past question: http://stackoverflow.com/questions/8682104/search-through-multiple-lists Thanks for helping. – chingchong Jan 04 '12 at 16:18
  • @chingchong: No, you did not post the complete traceback. The error message is not the traceback. – Lennart Regebro Jan 04 '12 at 16:49
  • @chingchong: No it was not. You got a traceback. You accuse me of lying, and then you lie yourself. Smooth. – Lennart Regebro Jan 04 '12 at 17:38
  • That is all that I received from IDLE when i ran the program except the part with the information on the file which is totally unnecessary and wouldn't help anyone so I didn't include it. – chingchong Jan 04 '12 at 18:52
  • @chingchong: If that is true, then this is one more reason never to use IDLE. And it does help. – Lennart Regebro Jan 04 '12 at 23:32
  • It helps you to know that the file is stored in documents rather than on the desktop? I honestly do not think that information has any effect on the Python code. – chingchong Jan 05 '12 at 22:48
  • @chingchong: I just installed IDLE and checked. IDLE gives you the complete traceback saying exactly which line the error was. So no more BS, please. – Lennart Regebro Jan 06 '12 at 10:34
  • 1
    @chingchong: -1 `Much Ado about Nothing` – Abhijit Jan 06 '12 at 11:13