如何找到两个Numpy数组之间的交集?
在这个问题中,我们将找到两个numpy数组之间的交集。两个数组的交集是在两个原始数组中都具有共同元素的数组
算法
Step 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find intersection between the arrays using the numpy.intersect1d() function. Step 4: Print the array of intersecting elements.
范例程式码
import numpy as np
array_1 = np.array([1,2,3,4,5])
print("Array 1:\n", array_1)
array_2 = np.array([2,4,6,8,10])
print("\nArray 2:\n", array_2)
intersection = np.intersect1d(array_1, array_2)
print("\nThe intersection between the two arrays is:\n", intersection)输出结果Array 1: [1 2 3 4 5] Array 2: [2 4 6 8 10] The intersection between the two arrays is: [2 4]
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短