如何找到两个Numpy数组之间的集差?
在这个程序中,我们将找到两个numpy数组的集差。我们将使用d()numpy库中的setdiff1函数。此函数采用两个参数:array1和array2,并返回array1中不在array2中的唯一值。
算法
Step 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find the set difference between these arrays using the setdiff1d() function. Step 4: Print the output.
示例代码
import numpy as np array_1 = np.array([2,4,6,8,10,12]) print("Array 1: \n", array_1) array_2 = np.array([4,8,12]) print("\nArray 2: \n", array_2) set_diff = np.setdiff1d(array_1, array_2) print("\nThe set difference between array_1 and array_2 is:\n",set_diff)输出结果
Array 1: [ 2 4 6 8 10 12] Array 2: [ 4 8 12] The set difference between array_1 and array_2 is: [ 2 6 10]
解释
数组1具有不在数组2中的元素2、6和10。因此[2610]是两个数组之间的集合差。