You can use numpy.intersect1d to get the intersection between 1d arrays. Please note that when you can find an intersection, “indexes are needed or use them to find yourself again !!!”
>>> a=np.random.randint(0,200,100) >>> b1=np.random.randint(0,100,50) >>> >>> np.intersect1d(b1,a) array([ 3, 9, 17, 19, 22, 23, 37, 53, 55, 58, 67, 85, 93, 94])
You may notice that using intersection is a more efficient way for a[np.in1d(a, b1)] in addition to calling in1d . The python function is forced to do additional indexing in order to better understand the following test:
import numpy as np s1=""" import numpy as np a=np.random.randint(0,200,100) b1=np.random.randint(0,100,50) np.intersect1d(b1,a) """ s2=""" import numpy as np a=np.random.randint(0,200,100) b1=np.random.randint(0,100,50) a[np.in1d(a, b1)] """ print ' first: ' ,timeit(stmt=s1, number=100000) print 'second : ',timeit(stmt=s2, number=100000)
Result:
first: 3.69082999229 second : 7.77609300613