Python中的 numpy.in1d()函数
numpy.in1d()
函数测试一维数组的每个元素是否也存在于第二个数组中,并返回一个与 arr1 相同长度的布尔数组,如果 arr1 的元素在 arr2 中,则返回 True,否则返回 False。
Syntax : numpy.in1d(arr1, arr2, assume_unique = False, invert = False)
Parameters :
arr1 : [array_like] Input array.
arr2 : [array_like] The values against which to test each value of arr1.
assume_unique : [bool, optional] If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False.
invert : [bool, optional] If True, the values in the returned array are inverted. Default is False.
Return : [ndarray, bool] The values arr1[in1d] are in arr2.
代码#1:
# Python program explaining
# numpy.in1d() function
# importing numpy as geek
import numpy as geek
arr1 = geek.array([0, 1, 2, 3, 0, 4, 5])
arr2 = [0, 2, 5]
gfg = geek.in1d(arr1, arr2)
print (gfg)
输出 :
[ True False True False True False True]
代码#2:
# Python program explaining
# numpy.in1d() function
# importing numpy as geek
import numpy as geek
arr1 = geek.array([0, 1, 2, 3, 0, 4, 5])
arr2 = [0, 2, 5]
gfg = geek.in1d(arr1, arr2, invert = True)
print (gfg)
输出 :
[False True False True False True False]