-1

I am using Python 3.x.

If I want to see if there are 8 numbers in any of three lists how would I do that in Python.

If I have:

list1 = [5, 6, 7, 2, 3, 5]
list2 = [2, 5, 8, 9, 1, 5]
list3 = [4, 6, 7 ,9, 2, 3]

How would I write this correctly in python?:

if 5 and 6 and 3 in (lists 1, 2, and 3)

The part in parenthesis is obviously not correct. How would I write the correct syntax for this in Python?

chingchong
  • 493
  • 2
  • 8
  • 11

1 Answers1

2

This is pretty simple and quite obvious to do. If you want to check if any number is in any list, you do this:

def any_number_in_any_lists(numbers, lists):
    for alist in lists:
        for number in numbers:
             if number in alist:
                  return True
    return False

If you want to check that all numbers are in all lists, you do this:

def all_number_in_all_lists(numbers, lists):
    for alist in lists:
        for number in numbers:
             if number not in alist:
                  return False
    return True

If you want to check for all numbers in any lists, it gets more complicated. The easiest and clearer is to have two methods:

def all_numbers_in_list(l, numbers):
    for n in numbers:
        if n is not in l:
             return False
    return True

def all_numbers_in_any_list(lists, numbers):
    for l in lists:
        if all_numbers_in_list(l, numbers):
            return True
    return False            

And you'd use the functions above like this:

if all_number_in_all_lists([3,5,6], [list1, list2, list3]):
    do something
else:
    do something else

Since you want less code, there are various "shortcuts" you can use. The shortcuts can make the code shorter, but will also make it less clear and harder to read, and hence they are not really a better choice. For example you can use the any function, and a generator expression:

 def any_number_in_any_lists(numbers, lists):
     for alist in lists:
         if any(number in alist for number in numbers):
             return True
     return False

You can even nest two of them:

def any_number_in_any_lists(numbers, lists):
    return any(any(number in alist for number in numbers) for alist in lists)

But understanding that code takes a long time and much more brain power. It is also significantly slower, as it will go through all lists, even if it finds a match in the first one. I can't think of any way that is not slower at the moment, but there may be one. But you are unlikely to find anything that is significantly faster and not significantly more confusing.

For the case of checking that all numbers are in all lists, you would do this:

def all_number_in_all_lists(numbers, lists):
    return all(all(number in alist for number in numbers) for alist in lists)

Which also, for some reason takes twice the time compared with the function above, even though there will be no shortcuts. It may have to do with the creation of intermediary lists.

This is often the case with Python: Simple is best.

Line by line explanation of the code you used, since you asked for it:

def all_numbers_in_list(l, numbers):

Defines a function, called "all_numbers_in_list", with the parameters "l" and "numbers".

    for n in numbers:

For every item in numbers, assign variable "n" to that number, and then do the following block.

        if n is not in l:

If the value of variable "n" is not in the list "l".

             return False

Exit the function with False as return value.

    return True

Exit the function with True as return value.

def all_numbers_in_any_list(lists, numbers):

Defines a function, called "all_numbers_in_any_list", with the parameters "lists" and "numbers".

    for l in lists:

For every item in lists, assign variable "l" to that number, and then do the following block.

        if all_numbers_in_list(l, numbers):

If calling the function "all_numbers_in_list" with the parameters "l" and "numbers" return True, do the following block:

            return True

Exit the function with True as return value.

    return False 

Exit the function with False as return value.

Did that help?

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • It is the short cuts that I would like to use. I don't want it to print but I want it to be an if statement. – chingchong Dec 30 '11 at 19:26
  • It would be more helpful if you filled in the part that I said I didn't know in my question. – chingchong Dec 30 '11 at 19:26
  • @chingchong: I *did* fill in the parts you didn't know. I changed the example to be an if statement. – Lennart Regebro Dec 30 '11 at 19:35
  • Isn't there anyway to do this without defining the number_in_lists? – chingchong Dec 30 '11 at 20:24
  • @chingchong: What is the problem with defining a function? But yes, you can do it without making the function. The function just makes it easier and clearer. – Lennart Regebro Dec 30 '11 at 22:38
  • How about: if 2 and 3 and 4 and 5 and 6 and 7 and 8 and 9 in list2 or list1? Would that work? – chingchong Dec 30 '11 at 23:02
  • I don't think your code is right. Looks like OP needs **every** number to be in at least one list but `number_in_lists([3, 1234], [list1, list2, list3])` is `True` because 3 is in a list. You should replace the outer `any` with `all` on last snippet (and fix the same thing on the others)... – JBernardo Dec 31 '11 at 05:23
  • What I need is all the numbers to be present but n a combined way. They can be anywhere in any of the three lists. – chingchong Dec 31 '11 at 16:47
  • @chingchong: You need them to be in sequence too? You didn't say that in your question, that changes things completely. And in fact, your question then becomes a duplicate: http://stackoverflow.com/questions/3847386/testing-if-a-list-contains-another-list-with-python In any case, you now have the answer to your original question in my answer. – Lennart Regebro Dec 31 '11 at 17:01
  • NO, NO, NO!!! I don't need them to be in any sequence. In fact in my previous comment I said: "They can be anywhere in any of the three lists." – chingchong Dec 31 '11 at 17:06
  • @chingchong: OK, good, I was worried there for a while. :-) Of course the code you posted wouldn't work. My answer gives you the code that *does* work. What is it you don't understand? – Lennart Regebro Dec 31 '11 at 20:34
  • The code in my comment doesn't work? I will try to cope with your answer then I suppose. It appeared to work just fine. – chingchong Dec 31 '11 at 23:27
  • @chingchong: The code in your comment would not work. "if 2" is always true. "if 2 and 3" is always true. " if 2 and 3 and 4 and 5 and 6 and 7 and 8" is always true. "or list1" is always true, unless list1 is empty. "9 in list2" is true if 9 is in list2. That's the only real test you have in that whole line. – Lennart Regebro Jan 01 '12 at 00:05
  • Your code draws an inexplicable error. I don't know why it is doing this but it is. Is there anything wrong with it? I am using 3.x. – chingchong Jan 02 '12 at 18:57
  • @chingchong: It will certainly be inexplicable when you don't explain what it is. No, there is nothing wrong with the code, start a new question. – Lennart Regebro Jan 03 '12 at 00:04
  • A new question?: "What is wrong with Lennart Regebro's answer?" I get an error saying: "argument type 'int' not iterable" – chingchong Jan 03 '12 at 02:03
  • @chingchong: Somehow I have to repeat everything I say to you several times. That is getting a bit frustrating. Again: There is nothing wrong with my answer. One of your lists, or your list of numbers, is in fact not a list at all, but a number. It is impossible to point out exactly what you are doing wrong, unless you paste in *your* code and the error you get, which you can't do in a comment, hence you should do a *new question* with a relevant title, such as "argument type 'int' not iterable". In which case you in fact will get up existing questions telling you exactly what that means. – Lennart Regebro Jan 03 '12 at 04:05
  • The problem with that though, the seeing old questions that could possibly help in my situation, is that they don't help with this situation. I don't know why this is happening because they are all lists. And basically at the end of your comment there you said I have to make a new question, which was my excellent idea in the first place, but don't actually do that because there are already questions on that topic. I don't see why you keep repeating yourself, it is certainly no fault of mine. And you definitely didn't say all that stuff after the "again." – chingchong Jan 03 '12 at 04:25
  • @chingchong: They are not all lists. The error message is explicitly telling you that something that you think is a list is not a list. At that point, saying that "they are all lists" is just stupid. No, they are not. You need to learn logic and debugging. If you don't learn that you'll never be able to do programming. – Lennart Regebro Jan 03 '12 at 07:05
  • @LennartRegebro: Perhaps you could further explain what your code does piece by piece? I am obviously a complete moron and I have no idea what is going on around me. Perhaps you could shine a light through this all encompassing idiocy. Thanks. I want to see if ALL the numbers are present but they could be found ANYWHERE throughout ANY of the lists, they don't all have to be in one of the lists. If you could just go line by line for that version of your answer I would be very grateful. You have been very helpful to me no matter how condescending you may be, so thanks again. – chingchong Jan 03 '12 at 19:51
  • @LennartRegebro: I have created a new question for you. It is located at: http://stackoverflow.com/questions/8721600/typeerror-int-object-is-not-iterable – chingchong Jan 04 '12 at 03:34
  • @chingchong: I'm sorry, the code is completely self-evident even to a newbie. I really can't help you here. You need to consider if you really want to learn programming. You can try and read this: http://openbookproject.net/thinkcs/python/english2e/ Maybe that will make some penny drop. – Lennart Regebro Jan 04 '12 at 08:05
  • @chingchong: OK, I went through the code, line by line, mostly to show how astonishingly trivial and self-evident the code is. – Lennart Regebro Jan 04 '12 at 17:02
  • Thanks for doing that, although I hope it isn't asking to much if you explain why you are doing the things you are doing. – chingchong Jan 04 '12 at 17:20
  • @chingchong: That's explained by the name of the functions. Maybe if you have questions you can ask them instead? If you really understand nothing of this code, give up. Why are you learning to program? – Lennart Regebro Jan 04 '12 at 17:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6394/discussion-between-lennart-regebro-and-chingchong) – Lennart Regebro Jan 04 '12 at 17:40