[Numpy-discussion] indices of values contained in a list
Gary Ruben
gruben@bigpond.net...
Sat Dec 12 06:51:42 CST 2009
np.setmember1d(a,b)
does the same as your
reduce(np.logical_or, [a == i for i in b])
but it's actually slower on my machine!
Gary R.
Ernest Adrogué wrote:
> Hi,
>
> 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
> situation?
>
> Cheers.
> Ernest
More information about the NumPy-Discussion
mailing list