[Numpy-discussion] Comparison of arrays

Francesc Alted faltet at pytables.org
Mon Feb 9 03:45:02 EST 2009


A Monday 09 February 2009, Nils Wagner escrigué:
> Hi all,
>
> I have two integer arrays of different shape, e.g.
>
> >>> a
>
> array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
>
> >>> b
>
> array([ 3,  4,  5,  6,  7,  8,  9, 10])
>
> How can I extract the values that belong to the array a
> exclusively i.e. array([1,2]) ?

One possible, fast solution is using Python sets:

In [45]: np.array(list(set(a) ^ set(b)))
Out[45]: array([1, 2])

Although this is suboptimal for very large arrays as it needs temporary 
space.

Cheers,

-- 
Francesc Alted



More information about the NumPy-Discussion mailing list