Given two lists:
In [518]: A
Out[518]: [3, 4, 2, 1, 7, 6, 5]
In [519]: B
Out[519]: [4, 6]
Every element in B
exists in A
, without exception.
I'd like to retrieve an array of indexes for B
as seen in A. For example, 4 is present in index 1 in A, and 6 is in position 5 for B
. My expected output is [1, 5]
for this scenario.
This is what I did to get the index:
In [520]: np.flatnonzero(np.in1d(a, b))
Out[520]: array([1, 5])
Unfortunately, this won't work in most other cases. For example, if B = [6, 4]
, my method still outputs [1, 5]
when it should output [5, 1]
.
Is there an efficient numpy way to get what I'm trying to achieve?