Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting indices of elements that are in another list in numpy

I have two numpy arrays and I'd like to get the indices of all elements in the first array that are in the second array. For example:

import numpy as np
x = np.array([0,1,1,2,3,4,5,5])
y = np.array([1,3])
# want to get np.array([1,2,4]) 

If y were a scalar, I could just do np.where(x == y). Is there an equivalent for arrays of values?

like image 707
Wesley Tansey Avatar asked Nov 21 '25 06:11

Wesley Tansey


1 Answers

You can numpy.where with numpy.in1d:

>>> np.where(np.in1d(x, y))
(array([1, 2, 4]),)
like image 68
Ashwini Chaudhary Avatar answered Nov 22 '25 21:11

Ashwini Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!