Numpy: For every element in one array, find the index in another array Numpy: For every element in one array, find the index in another array python python

Numpy: For every element in one array, find the index in another array


I want to suggest one-line solution:

indices = np.where(np.in1d(x, y))[0]

The result is an array with indices for x array which corresponds to elements from y which were found in x.

One can use it without numpy.where if needs.


As Joe Kington said, searchsorted() can search element very quickly. To deal with elements that are not in x, you can check the searched result with original y, and create a masked array:

import numpy as npx = np.array([3,5,7,1,9,8,6,6])y = np.array([2,1,5,10,100,6])index = np.argsort(x)sorted_x = x[index]sorted_index = np.searchsorted(sorted_x, y)yindex = np.take(index, sorted_index, mode="clip")mask = x[yindex] != yresult = np.ma.array(yindex, mask=mask)print result

the result is:

[-- 3 1 -- -- 6]


How about this?

It does assume that every element of y is in x, (and will return results even for elements that aren't!) but it is much faster.

import numpy as np# Generate some example data...x = np.arange(1000)np.random.shuffle(x)y = np.arange(100)# Actually preform the operation...xsorted = np.argsort(x)ypos = np.searchsorted(x[xsorted], y)indices = xsorted[ypos]