[Numpy-discussion] extract elements of an array that are contained in another array?

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Jun 4 08:35:05 EDT 2009

On Thu, Jun 4, 2009 at 8:23 AM, Alan G Isaac <aisaac at american.edu> wrote:
> a[(a==b[:,None]).sum(axis=0,dtype=bool)]

this is my preferred way when b is small and has unique elements.
if the elements in b are not unique, then be can be replaced by np.unique(b)
If b is large this creates a huge intermediate array

The advantage of the new setmember1d_nu is that it handles large b
very efficiently. My try on it was more than 10 times slower than the
proposed solution for larger arrays.


> hth,
> Alan Isaac

More information about the NumPy-Discussion mailing list