+2 votes
in Programming Languages by (54.2k points)
Without converting Numpy arrays to sets, what is the approach to find the set difference of two Numpy arrays or lists?

1 Answer

+1 vote
by (197k points)
selected by
Best answer

You can use the setdiff1d() function of Numpy that returns elements in array1 that are not in array2. Here is an example:

>>> import numpy as np
>>> a=np.array([11,12,13,14,15])
>>> b=np.array([14,15,16,17,18])
>>> np.setdiff1d(a, b)
array([11, 12, 13])
>>> np.setdiff1d(b, a)
array([16, 17, 18])