[Numpy-discussion] indices of values contained in a list
Sat Dec 12 06:51:42 CST 2009
does the same as your
reduce(np.logical_or, [a == i for i in b])
but it's actually slower on my machine!
Ernest Adrogué wrote:
> 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