[Numpy-discussion] Comparison of arrays

Brent Pedersen bpederse at gmail.com
Mon Feb 9 11:22:35 EST 2009


On Mon, Feb 9, 2009 at 6:02 AM, Neil <neilcrighton at gmail.com> wrote:
>
>> > 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
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

there's also np.setdiff1d() which does the above in a single line.
-brent



More information about the NumPy-Discussion mailing list