0

How could I use an array from numpy and return an array with the index of each sorted element of the original array?

For example, In: [10, 5, 13, 2], and I expect the Output will be [2, 1, 3, 0].

The "argsort" function doesn't work for me, because it returns an output that I don't want (Output [3 1 0 2]), that is it sorts inside and returns the array but with the elements sorted, and I need it with the order of the original array.

azro
  • 53,056
  • 7
  • 34
  • 70
Ande P
  • 1
  • See: https://stackoverflow.com/questions/54459554/numpy-find-index-in-sorted-array-in-an-efficient-way. Basically: you can use **two** calls to `argsort` to do this. (Notice how in your example, `np.argsort([3, 1, 0, 2])` would give `[2, 1, 3, 0]` which is your intented output.) – slothrop Jul 15 '23 at 08:08
  • Do you have to use numpy? This is trivial for a plain Python list – DarkKnight Jul 15 '23 at 08:36

0 Answers0