-3

Let's say I have several lists. If the number 1 appears at least once, I want to return it as True. If there are no instances of 1 appearing, then it is False. So let's say I have several lists.

[2.0,2.0]
[1.0, 1.0, 2.0, 2.0]
[1.0, 2.0]
[3.0, 1.0]
[3.0, 3.0, 1.0, 1.0]
[3.0, 3.0, 3.0, 3.0]

The outputs would be:

False
True
True
True
True
False

How can I accomplish this?

EDIT: The code example

def is_walkable(i,j,current_board):
    # find all the diagonal neighbours of (i,j) that are on board and put them into a list
    diagonal_neighbors = [(i+1,j-1),(i+1,j+1),(i-1,j-1),(i-1,j+1)]
    # loop over the list of neighbours to see if any of them is empty 
    neighbor_values = []
    for neighbor in diagonal_neighbors:
        row = neighbor[0]
        col = neighbor[1]
    # if there is an empty neighbour, return True, otherwise return False
        if on_board(row,col,current_board):
            neighbor_values.append(current_board[row,col])
        for neighbor in neighbor_values:
            if any(neighbor_values) == 1:
                return True
            else:
                return False

and the testing code is:

current_board = initial_board(empty_board)
print(is_walkable(0,1,current_board) == False)
print(is_walkable(2,1,current_board) == True)
print(is_walkable(2,7,current_board) == True)
print(is_walkable(5,0,current_board) == True)
print(is_walkable(5,6,current_board) == True)
print(is_walkable(6,1,current_board) == False)

All outputs should be "True." This is for a game of checkers on an 8x8 board.

Gian Batayola
  • 73
  • 1
  • 11
  • `['1' in a_list for a_list in list_of_lists]` – Joran Beasley Mar 11 '20 at 03:47
  • You say "all outputs should be `True`", but your code shows you printing `== False` for some and `== True` for others. What exactly are you testing for here - what is the expected result and what is the result you're getting instead? – Grismar Mar 11 '20 at 04:10
  • The testing code was provided to me. I didn't type it and I know it's not very intuitive, but something similar has worked before. Essentially, if the function's correct output is false and we set it == false, then the output is true. – Gian Batayola Mar 11 '20 at 04:19
  • My results come out as False True True True True False, but should come out as True True True True True True. I can send the whole notebook if needed. – Gian Batayola Mar 11 '20 at 04:20

1 Answers1

0

I think this is what you're after:

xss = [
    [2.0, 2.0],
    [1.0, 1.0, 2.0, 2.0],
    [1.0, 2.0],
    [3.0, 1.0],
    [3.0, 3.0, 1.0, 1.0],
    [3.0, 3.0, 3.0, 3.0],
]

print(any(1.0 in xs for xs in xss))

any will cause the generator 1.0 in xs for xs in xss to keep checking lists in xss until one contains 1.0 with 1.0 in xs.

Check the documentation on any and generators and comprehensions if you want to fully understand the solution, but I think the solution is clear and easy to understand by itself.

If you just want a list of boolean values for the lists:

has_one_xss = [1.0 in xs for xs in xss]

print(has_one_xss)

Prints:

[False, True, True, True, True, False]
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Thank you! However, these lists are not contained in a list. The lists are independent of each other. How do I do this one at a time? So if I'm just looking at [2.0,2.0] by itself, how do I set it to false? – Gian Batayola Mar 11 '20 at 03:58
  • Please update your question with an actual code example, so I don't have to guess where your lists are and what your variables are called. – Grismar Mar 11 '20 at 03:59