MCPcopy Create free account
hub / github.com/numpy/numpy / setdiff1d

Function setdiff1d

numpy/lib/arraysetops.py:940–981  ·  view source on GitHub ↗

Find the set difference of two arrays. Return the unique values in `ar1` that are not in `ar2`. Parameters ---------- ar1 : array_like Input array. ar2 : array_like Input comparison array. assume_unique : bool If True, the input arrays are both

(ar1, ar2, assume_unique=False)

Source from the content-addressed store, hash-verified

938
939@array_function_dispatch(_setdiff1d_dispatcher)
940def setdiff1d(ar1, ar2, assume_unique=False):
941 """
942 Find the set difference of two arrays.
943
944 Return the unique values in `ar1` that are not in `ar2`.
945
946 Parameters
947 ----------
948 ar1 : array_like
949 Input array.
950 ar2 : array_like
951 Input comparison array.
952 assume_unique : bool
953 If True, the input arrays are both assumed to be unique, which
954 can speed up the calculation. Default is False.
955
956 Returns
957 -------
958 setdiff1d : ndarray
959 1D array of values in `ar1` that are not in `ar2`. The result
960 is sorted when `assume_unique=False`, but otherwise only sorted
961 if the input is sorted.
962
963 See Also
964 --------
965 numpy.lib.arraysetops : Module with a number of other functions for
966 performing set operations on arrays.
967
968 Examples
969 --------
970 >>> a = np.array([1, 2, 3, 2, 4, 1])
971 >>> b = np.array([3, 4, 5, 6])
972 >>> np.setdiff1d(a, b)
973 array([1, 2])
974
975 """
976 if assume_unique:
977 ar1 = np.asarray(ar1).ravel()
978 else:
979 ar1 = unique(ar1)
980 ar2 = unique(ar2)
981 return ar1[in1d(ar1, ar2, assume_unique=True, invert=True)]

Callers 4

test_setdiff1dMethod · 0.90
test_setdiff1d_uniqueMethod · 0.90
test_manywaysMethod · 0.90

Calls 3

uniqueFunction · 0.70
in1dFunction · 0.70
ravelMethod · 0.45

Tested by 4

test_setdiff1dMethod · 0.72
test_setdiff1d_uniqueMethod · 0.72
test_manywaysMethod · 0.72