获取两个numpy数组之间的公共项

a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
np.intersect1d(a,b)
# > array([2, 4])

numpy.intersect1d(ar1, ar2, assume_unique=False, return_indices=False)[source]
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 both assumed to be unique, which can speed up the calculation. Default is False.
默认是False,如果是True,假定输入的数组中元素是没有重复的

return_indices : bool
If True, the indices which correspond to the intersection of the two arrays are returned.
The first instance of a value is used if there are multiple. Default is False.
默认是False,如果是True,返回共同元素的索引位置,因为返回共同元素是排序后的,所以索引位置是排序后的元素位置。
如果共同元素在一个数组中有多次出现,只返回第一次出现的索引位置

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值