[Numpy-discussion] indices of values contained in a list

Ernest Adrogué eadrogue@gmx....
Sat Dec 12 06:00:29 CST 2009


Suppose I have a flat array, and I want to know the
indices corresponding to values contained in a list
of arbitrary lenght.

Intuitively I would have done:

a = np.array([1,2,3,4])
np.nonzero(a in (0,2,4))

However the "in" operator doesn't work element-wise,
instead it compares the whole array with each member
of the list.

I have found that this does the trick:

b = (0,2,4)
reduce(np.logical_or, [a == i for i in b])

then pass the result to np.nonzero to get the indices,
but, is there a numpy function that can handle this


More information about the NumPy-Discussion mailing list