获取两个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,返回共同元素的索引位置,因为返回共同元素是排序后的,所以索引位置是排序后的元素位置。
如果共同元素在一个数组中有多次出现,只返回第一次出现的索引位置

Original: https://blog.csdn.net/weixin_48419914/article/details/123757604
Author: 黄佳俊、
Title: 获取两个numpy数组之间的公共项

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/759234/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球