0

I have a small list:

moveless = [0,11,30,31,20,21,22,23,24,25,26,27,28,29]

And, for example, an array like so (it will always come nested in a list like this, but there will often be tens/hundreds of thousands of them, and any number from -1 to 31 can be in any spot):

starting_board = [([[ 6, -1, -1,  11, 0,  11, -1,  -1,  -1, -1],
       [-1, -1, -1, -1, -1, -1, -1,  11, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, 2],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, 20, 31, -1, 25, -1, -1, -1]])]

And I need to determine if any of the elements of this array are not in moveless. So if it parsed it and got to 6, it could stop.

I feel like there must be a faster and more elegant/pythonic way to do this than just a couple for loops, but my checking through the numpy docs/elsewhere on this site hasn't revealed anything terribly useful.

2 Answers 2

2

Using isin()

starting_board = [([[ 6, -1, -1,  11, 0,  11, -1,  -1,  -1, -1],
       [-1, -1, -1, -1, -1, -1, -1,  11, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, 1, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, 2],
       [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
       [-1, -1, -1, 20, 31, -1, 25, -1, -1, -1]])]
moveless = [0,11,30,31,20,21,22,23,24,25,26,27,28,29]
print(np.isin(starting_board, moveless))

Output:

[[[False False False  True  True  True False False False False]
  [False False False False False False False  True False False]
  [False False False False False False False False False False]
  [False False False False False False False False False False]
  [False False False False False False False False False False]
  [False False False False False False False False False False]
  [False False False False False False False False False False]
  [False False False  True  True False  True False False False]]]
Sign up to request clarification or add additional context in comments.

Comments

1

If all you need to know is if any of the elements are not in the list, just do a set comparison:

any_items_not_in_moveless = {element for row in starting_board[0] for element in row} - set(moveless)
# OR
any_items_not_in_moveless = set(np.asarray(starting_board[0]).ravel()) - set(moveless)

As @Chirag points out, you could also leverage Numpy's isin function. Which solution is faster/better for you is something you'd need to figure out for your own case:

any_items_not_in_moveless = (~np.asarray(starting_board[0]).isin(moveless)).any()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.