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]))
> 100000 loops, best of 3: 14 µs per loop
> >> timeit [i for i, z in enumerate(a) if z in b]
> 100000 loops, best of 3: 3.43 µs per loop
> 
> Looping over a instead of b is faster if len(a) is much less than len(b):
> 
> >> a = np.random.randint(0,100,10000)
> >> b = tuple(set(a[:50].tolist()))
> >> len(b)
>    41
> >> timeit np.nonzero(reduce(np.logical_or, [a == i for i in b]))
> 100 loops, best of 3: 2.65 ms per loop
> >> timeit [i for i, z in enumerate(a) if z in b]
> 10 loops, best of 3: 37.7 ms per loop

Nice. Well speed was not critical in this case, sometimes
is good to know alternative ways of doing things.
Thanks for your suggestions.

Ernest
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to