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

1 Answer

0 votes
by (29.1k points)
edited by

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