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 arr
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.
2009/12/12 Ernest Adrogué :
> 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
12/12/09 @ 08:16 (-0800), thus spake Keith Goodman:
> If a and b are as short as in your example, which I doubt, here's a faster
> way:
>
> >> timeit np.nonzero(reduce(np.logical_or, [a == i for i in b]))
> 10 loops, best of 3: 14 µs per loop
> >> timeit [i for i, z in enumerate(a) if z in b]