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

Function intersect1d

numpy/lib/arraysetops.py:374–469  ·  view source on GitHub ↗

Find the intersection of two arrays. Return the sorted, unique values that are in both of the input arrays. Parameters ---------- ar1, ar2 : array_like Input arrays. Will be flattened if not already 1D. assume_unique : bool If True, the input arrays are bot

(ar1, ar2, assume_unique=False, return_indices=False)

Source from the content-addressed store, hash-verified

372
373@array_function_dispatch(_intersect1d_dispatcher)
374def intersect1d(ar1, ar2, assume_unique=False, return_indices=False):
375 """
376 Find the intersection of two arrays.
377
378 Return the sorted, unique values that are in both of the input arrays.
379
380 Parameters
381 ----------
382 ar1, ar2 : array_like
383 Input arrays. Will be flattened if not already 1D.
384 assume_unique : bool
385 If True, the input arrays are both assumed to be unique, which
386 can speed up the calculation. If True but ``ar1`` or ``ar2`` are not
387 unique, incorrect results and out-of-bounds indices could result.
388 Default is False.
389 return_indices : bool
390 If True, the indices which correspond to the intersection of the two
391 arrays are returned. The first instance of a value is used if there are
392 multiple. Default is False.
393
394 .. versionadded:: 1.15.0
395
396 Returns
397 -------
398 intersect1d : ndarray
399 Sorted 1D array of common and unique elements.
400 comm1 : ndarray
401 The indices of the first occurrences of the common values in `ar1`.
402 Only provided if `return_indices` is True.
403 comm2 : ndarray
404 The indices of the first occurrences of the common values in `ar2`.
405 Only provided if `return_indices` is True.
406
407
408 See Also
409 --------
410 numpy.lib.arraysetops : Module with a number of other functions for
411 performing set operations on arrays.
412
413 Examples
414 --------
415 >>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
416 array([1, 3])
417
418 To intersect more than two arrays, use functools.reduce:
419
420 >>> from functools import reduce
421 >>> reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
422 array([3])
423
424 To return the indices of the values common to the input arrays
425 along with the intersected values:
426
427 >>> x = np.array([1, 1, 2, 3, 4])
428 >>> y = np.array([2, 1, 4, 6])
429 >>> xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True)
430 >>> x_ind, y_ind
431 (array([0, 2, 4]), array([1, 0, 2]))

Callers 4

test_intersect1dMethod · 0.90
test_manywaysMethod · 0.90

Calls 4

sortMethod · 0.80
uniqueFunction · 0.70
ravelMethod · 0.45
argsortMethod · 0.45

Tested by 4

test_intersect1dMethod · 0.72
test_manywaysMethod · 0.72