查找两个 NumPy 数组之间的共同值
在 NumPy 中,我们可以在 intersect1d() 的帮助下找到两个数组之间的共同值。它将接受参数两个数组,并将返回一个数组,其中将出现所有常见元素。
Syntax: numpy.intersect1d(array1,array2)
Parameter :Two arrays.
Return :An array in which all the common element will appear.
示例 1:
Python
import numpy as np
ar1 = np.array([0, 1, 2, 3, 4])
ar2 = [1, 3, 4]
# Common values between two arrays
print(np.intersect1d(ar1, ar2))
Python
import numpy as np
ar1 = np.array([12, 14, 15, 16, 17])
ar2 = [2, 4, 5, 6, 7, 8, 9, 12]
# Common values between two arrays
print(np.intersect1d(ar1, ar2))
输出:
[1,3,4]
示例 2:
Python
import numpy as np
ar1 = np.array([12, 14, 15, 16, 17])
ar2 = [2, 4, 5, 6, 7, 8, 9, 12]
# Common values between two arrays
print(np.intersect1d(ar1, ar2))
输出:
[12]