3

so I've been having trouble checking if the number 0 is in a group of lists within another list. These rows make up the maze for a pacman type game I'm making so the point of this is to check if pacman has eaten all of the coins.

Here's my code:

row1 =[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
row2 =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
row3 =[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1]
row4 =[1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1]
row5 =[1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1]
row6 =[1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1]
row7 =[1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1]
row8 =[1,0,0,0,0,1,0,1,1,1,1,1,0,1,0,0,0,0,1]
row9 =[1,1,1,1,0,1,0,0,0,1,0,0,0,1,0,1,1,1,1]
row10=[3,3,3,1,0,1,1,1,0,1,0,1,1,1,0,1,3,3,3]
row11=[3,3,3,1,0,1,0,0,0,0,0,0,0,1,0,1,3,3,3]
row12=[3,3,3,1,0,1,0,1,4,4,4,1,0,1,0,1,3,3,3]
row13=[1,1,1,1,0,1,0,1,3,3,3,1,0,1,0,1,1,1,1]
row14=[3,3,3,3,0,0,0,1,3,3,3,1,0,0,0,3,3,3,3]
row15=[1,1,1,1,0,1,0,1,5,5,5,1,0,1,0,1,1,1,1]
row16=[3,3,3,1,0,1,0,3,3,3,3,3,0,1,0,1,3,3,3]
row17=[3,3,3,1,0,1,0,1,1,1,1,1,0,1,0,1,3,3,3]
row18=[3,3,3,1,0,1,0,1,1,1,1,1,0,1,0,1,3,3,3]
row19=[1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1]
row20=[1,0,0,0,0,1,1,1,0,1,0,1,1,1,0,0,0,0,1]
row21=[1,0,1,1,0,0,0,0,0,3,0,0,0,0,0,1,1,0,1]
row22=[1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1]
row23=[1,2,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,2,1]
row24=[1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1]
row25=[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
row26=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

maze = [row1,row2,row3,row4,row5,row6,row7,row8,row9,row10,row11,row12,
        row13,row14,row15,row16,row17,row18,row19,row20,row21,row22,
        row23,row24,row25,row26]

So I have tried if 0 not in maze: But the if statement will execute whether 0 is in (maze) or not.

Any help would be appreciated, thanks.

DYZ
  • 55,249
  • 10
  • 64
  • 93

5 Answers5

7

You'll have to loop through each list in maze and check whether there is a 0 or not:

if not any(0 in i for i in maze):
    ...

The great thing about the any() function is that it stops looping through maze once it finds a 0.

TerryA
  • 58,805
  • 11
  • 114
  • 143
1

With your first attempt, your only testing for 0 in the top-level list. Of course, the top-level list only has lists, thus, your test fails.

You must iterate through each sublist in maze. You could use a manually for loop, which is a valid option:

def has_zero(maze):
    for sublist in maze:
        for el in sublist:
            if el == 0:
                return True
    return False

but Python provides a better way with the any() function(which is basically a better implementation of the above function):

Return True if any element of the iterable is true. If the iterable is empty, return False[...].

But you must also use the not operator(know as ! in other languages) to negate the possibly return true value of any(). Thus, the full solution would be:

not any(el == 0 for sublist in maze for el in sublist)
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
1

any was already mentioned but you could also flatten your lists (for example using itertools.chain.from_iterable) and check if the item is contained:

from itertools import chain

if 0 in chain.from_iterable(lst):
    # do something

Like any this stops as soon as one is found. Just to mention it because it's something that is often overlooked: generators and iterators (like chain) generally support the in operator.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
0

Loop through the sublist. Also use any()

if any(0 in sublist for sublist in maze)

It will check if there is any 0 in the list

Aatish Sai
  • 1,647
  • 1
  • 26
  • 41
-1

as long as i understand your problem you can use the following code:

a = [1,1,1]
b = [1,0,1]
c = [0,1,2]
d = [a,b,c]

for i in d:    
  if i.__contains__(0):
    print("yes") 
Community
  • 1
  • 1
S_Ymln
  • 421
  • 3
  • 14
  • You should almost never use `__contains__` or anything else enclosed in double underscores in your code. – DYZ Jan 16 '17 at 01:27
  • thanks for the advice i'm all new here will check it. – S_Ymln Jan 16 '17 at 01:32
  • You can explain a bit more about what the code does. – JLT Jan 16 '17 at 01:35
  • @DYZ Do you have any source to backup your claim? I was always under the impression one shouldn't use functions/attributes/methods starting with a **single underscore**. – MSeifert Jan 16 '17 at 01:40
  • @MSeifert http://stackoverflow.com/questions/8689964/why-do-some-functions-have-underscores-before-and-after-the-function-name Quote: "While you can call these methods directly ([10, 20].__len__() for example), the presence of the underscores is a hint that these methods are intended to be invoked indirectly (len([10, 20]) for example)." – DYZ Jan 16 '17 at 01:43
  • That's not official documentation! For example [PEP8](https://www.python.org/dev/peps/pep-0008/) (official) states that "`__double_leading_and_trailing_underscore__`: "magic" objects or attributes that live in user-controlled namespaces. [...]. Never invent such names; **only use them as documented**." They explicitly mention that they can be used (as documented). – MSeifert Jan 16 '17 at 01:46