在这个问题中,我们将找到两个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]