如何找到两个 Numpy 数组之间的交集?
pythonnumpyserver side programmingprogramming更新于 2024/9/21 21:32:17
在这个问题中,我们将找到两个 Numpy 数组之间的交集。两个数组的交集是两个原始数组中元素相同的数组
算法
步骤 1:导入 Numpy。 步骤 2:定义两个 Numpy 数组。 步骤 3:使用 numpy.intersect1d() 函数找到数组之间的交集。 步骤 4:打印相交元素的数组。
示例代码
import numpy as np
array_1 = np.array([1,2,3,4,5])
print("数组 1:\n", array_1)
array_2 = np.array([2,4,6,8,10])
print("\n数组 2:\n", array_2)
intersection = np.intersect1d(array_1, array_2)
print("\n两个数组的交集为:\n", Intersection)
输出
数组 1: [1 2 3 4 5] 数组 2: [2 4 6 8 10] 两个数组的交集为: [2 4]
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

