[Numpy-discussion] Comparison of arrays

Neil neilcrighton@gmail....
Mon Feb 9 08:02:12 CST 2009


> > 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]) ?
> 

You could also use numpy.setmember1d to get a boolean mask that selects 
the values:

In [21]: a = np.array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
In [22]: b = np.array([ 3,  4,  5,  6,  7,  8,  9, 10])
In [23]: ismember = np.setmember1d(a,b)
In [24]: a[~ismember]
Out[24]: array([1, 2])


Neil




More information about the Numpy-discussion mailing list