如何找到两个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]是两个数组之间的集合差。
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短