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

2009-12-12 Thread 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 element-wise, instead it compares the whole arr

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

2009-12-12 Thread Gary Ruben
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.

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

2009-12-12 Thread Keith Goodman
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

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

2009-12-13 Thread Ernest Adrogué
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]