I have two numpy arrays such as
import numpy as np
x = np.array([3, 1, 4])
y = np.array([4, 3, 2, 1, 0])
each containing unique values. The values in x
are guaranteed to be a subset of those in y
.
I would like to find the index of each element of x
in the array y
.
In the array above, this would be
[1, 3, 0]
So far I have been finding the indices one at a time in a loop:
idxs = []
for val in x:
idxs.append(np.argwhere(y == val)[0,0])
But this is slow when my arrays are large.
Is there a more efficient way to do this?