numpy.intersect1d

本文介绍了如何使用NumPy库中的intersect1d函数来找出两个数组之间的交集元素。通过示例展示了当输入数组可能存在重复元素时如何正确地获取唯一且排序后的交集。此外,还介绍了如何利用functools.reduce函数来对多个数组进行交集运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy.intersect1d

numpy. intersect1d ( ar1, ar2, assume_unique=False )

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.

assume_unique : bool

If True, the input arrays are both assumed to be unique, whichcan speed up the calculation. Default is False.

Returns:

intersect1d : ndarray

Sorted 1D array of common and unique elements.

See also

numpy.lib.arraysetops
Module with a number of other functions for performing set operations on arrays.

Examples

>>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
array([1, 3])

To intersect more than two arrays, use functools.reduce:

>>> from functools import reduce
>>> reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
array([3])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值