0

I have the following two arrays:

array1 = numpy.asarray(['x', 'a', 'y', 'b', 'c'], dtype=str)
array2 = numpy.asarray(['c','y','a'], dtype=str)

as a result I need to have an array that contains the indexes from array1 of the elements that are present in array2. Furthermore, the resulting array has to have the order of the labels from array2 like this:

In [1]: result_nd
Out[2]: array([4, 2, 1])

What would be the most concise and computationally effective way of achieving this (the array1 can have millions of elements)?

Divakar
  • 218,885
  • 19
  • 262
  • 358
Riko
  • 155
  • 1
  • 3
  • 10
  • @Divakar I disagree that my question is a duplicate. None of the answers in the attached post [link](https://stackoverflow.com/questions/33678543/finding-indices-of-matches-of-one-array-in-another-array) answers my question. The solution with searchsorted will return wrong indexes and the solution using numpy.in1d will not preserve the order of the element from array2. I have extended my question to demonstrate this. – Riko May 23 '19 at 08:22
  • Please read the entire post. Follow the generic case solution. After you are satisfied, please revert back the edits. – Divakar May 23 '19 at 08:40
  • @Divaker ok, thanks. The general solutions seem to work. so I updated my question. But the provided post does not really explain what those general solutions do under the hood and why do they work. It would be nice to have a step-by-step explanation. – Riko May 23 '19 at 11:07
  • Don't make edits that are not adding any material to the question. – Divakar May 23 '19 at 15:38

0 Answers0