10

I thought that in would be good for this but it returns true in places where it shouldn't. For example:

import numpy as np

a = np.array([])

for i in range(3):
    for j in range(3):
        a = np.append(a,[i,j])
a = np.reshape(a,(9,2))
print(a)

print([[0,40]] in a)

will print true. I cannot understand why it does this... is it because 0 is in the list? I'd like to have something that only prints true if the entire array is in the list.

I want to have my list

[[0,1],
[0,2]]

and only return true if exactly [0,x] (same shape same order) is in it.

2
  • Side note: you can more efficiently construct a by three = np.arange(3.0); np.array([np.repeat(three, 3), np.tile(three, 3)]).T. Commented Jun 10, 2018 at 10:40
  • 1
    This is a related question. Commented Jun 10, 2018 at 13:34

3 Answers 3

6

You can do it this way:

([0, 40] == a).all(1).any()

The first step is to compute a 2D boolean array of where the matches are. Then you find the rows where all elements are true. Then you check if any rows are fully matching.

Sign up to request clarification or add additional context in comments.

Comments

1

This code can help you:

my_list = [0, 40]
print(all(b in a for b in my_list))

Comments

0

You can use np.isin(a_array, b_array) ,it returned a boolean array. for example:

import numpy as np
a_array =np.array([1,2,3,4])
b_array = np.array([3,4,5])
bool_a_b = np.isin(a_array, b_array)
print(bool_a_b)

Output:

[False False True True]

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.