0
np.where(A ==2)[0]

gives the indexes of A where elements are equal to 2.

How do you generalize to a list of possible values?

I am looking for something like:

np.where(A in ([2,3,6,8]))[0]
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Mauro Gentile
  • 1,463
  • 6
  • 26
  • 37
  • Possible duplicate of [Numpy - check if elements of a array belong to another array](https://stackoverflow.com/questions/37913957/numpy-check-if-elements-of-a-array-belong-to-another-array) – miradulo Feb 24 '18 at 22:35
  • Short story: use `np.isin` for your mask nowadays. – miradulo Feb 24 '18 at 22:35
  • Or another duplicate: [Numpy mask based on if a value is in some other list](https://stackoverflow.com/questions/13629061/numpy-mask-based-on-if-a-value-is-in-some-other-list) – miradulo Feb 24 '18 at 22:40

1 Answers1

1

Since NumPy 1.13 you can use the isin function.

In prior versions there was in1d.

Test:

A = np.array([1, 2, 3, 4, 5])
print(np.isin(A, [2, 3, 6, 8]))

Results:

[False  True  True False False]
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135