numpy.in1d() function in Python (original) (raw)

Last Updated : 17 May, 2020

numpy.in1d() function test whether each element of a 1-D array is also present in a second array and return a boolean array the same length as arr1 that is True where an element of arr1 is in arr2 and False otherwise.

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.

Code #1 :

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)

Output :

[ True False True False True False True]

Code #2 :

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)

Output :

[False True False True False True False]

Similar Reads