I have a large (millionth) array of identification numbers ids, and I want to find indexes where idsanother array of targets ( targets) exists in the array . For example, if
ids = [22, 5, 4, 0, 100]
targets = [5, 0]
then I want to get the result:
>>> [1,3]
If I pre-sort the array ids, then it is easy to find matches using numpy.searchsorted, for example.
>>> ids = np.array([22, 5, 4, 0, 100])
>>> targets = [5, 0]
>>> sort = np.argsort(ids)
>>> ids[sort]
[0,4,5,22,100]
>>> np.searchsorted(ids, targets, sorter=sort)
[2,0]
But how can I find the inverse mapping for the "unsort" of this result? That is to match the sorted records in the [2,0]back where they were before: [1,3].
source
share