Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the non-intersecting values of two arrays

If I have two numpy arrays and want to find the the non-intersecting values, how do I do it?

Here's a short example of what I can't figure out.

a = ['Brian', 'Steve', 'Andrew', 'Craig']
b = ['Andrew','Steve']

I want to find the non-intersecting values. In this case I want my output to be:

['Brian','Craig']

The opposite of what I want is done with this:

c=np.intersect1d(a,b)

which returns

['Andrew' 'Steve']
like image 778
itjcms18 Avatar asked Aug 09 '14 17:08

itjcms18


People also ask

What is the intersection of two arrays?

The intersection is a list of common elements present in both arrays. The elements in the output can be in any order.

How do you find the intersection of two arrays in CPP?

Similarly, intersection of two arrays will be denoted by A ∩ B. It is an array of the elements that are present in both the given arrays. For this, we will traverse through the elements of the first array one by one. Simultaneously we will be checking if that element is present in the second array or not.


1 Answers

You can use setxor1d. According to the documentation:

Find the set exclusive-or of two arrays.
Return the sorted, unique values that are in only one (not both) of the input arrays.

Usage is as follows:

import numpy

a = ['Brian', 'Steve', 'Andrew', 'Craig']
b = ['Andrew','Steve']

c = numpy.setxor1d(a, b)

Executing this will result in c having a value of array(['Brian', 'Craig']).

like image 189
Robby Cornelissen Avatar answered Sep 27 '22 23:09

Robby Cornelissen