1

b =np.ndarray(shape=(5,5), dtype=int, order='F')
Out[142]: 
array([[       1,    65536,        0,        0,        0],
       [       0,        0,        1,    65536,        0],
       [16777216,        0,        0,        0,        1],
       [       0,      256, 16777216,        0,        0],
       [       0,        0,        0,      256, 16777216]])

and I have a list, such as :

a = [1,23,4,5,20,0...]

I would like to check for each index if the value in a is in the corresponding row in b , as np.isin(a[0], b[0]) and to receive Boolean vector for all the rows (if a[i] is in b[i]).

The length of a is equal the length of b (amount of rows).

1 Answer 1

1

Here's one way using np.ndarray.any. Just take care to align dimensions to allow broadcasting.

np.random.seed(0)

b = np.random.randint(0, 10, (5, 5))
a = np.random.randint(0, 10, 5)

print(a, b, sep='\n'*2)

[3 0 3 5 0]

[[5 0 3 3 7]
 [9 3 5 2 4]
 [7 6 8 8 1]
 [6 7 7 8 1]
 [5 9 8 9 4]]

c = (a[:, None] == b).any(1)

print(c)

array([ True, False, False, False, False], dtype=bool)
Sign up to request clarification or add additional context in comments.

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.