0

I have a list of floats and I want to know their index if I were to sort them. Numpy has a function that almost does what I want: argsort. Here is an example from the manual:

x = np.array([3, 1, 2])

np.argsort(x) # array([1, 2, 0])

For what I need it should rather be array([2, 0, 1])

This seems to be a simple question yet I found no one online that wants to do that. What do I need?

cigien
  • 57,834
  • 11
  • 73
  • 112
Qise
  • 192
  • 6
  • I posted an answer to yours but it was closed, saying that the question was a duplicate, so I posted there https://stackoverflow.com/a/71284900/12750353. – Bob Feb 27 '22 at 12:51
  • Thank you; that's a quite nice solution. I'm surprise this function is not directly implemented in numpy (maybe I just did nout found it) – Qise Feb 27 '22 at 14:38
  • No, it is not implemented, but I don't think it is a problem since it is not often necessary, and if you need you can easily do with the existing functions. Could at vote my answer there, let's see if they reopen this question here, thank you. – Bob Feb 27 '22 at 14:45
  • This is not sorting (or inverse sorting), but rather ranking each element in the array. You're not looking for the index of each value if the array were sorted, but rather where each element falls relative to the others in the array. The [accepted answer](https://stackoverflow.com/a/5284703/15497888) in the canonical is functionally identical to [Bob's answer](https://stackoverflow.com/a/71284900/15497888). There is a collection of other options and their relative timings in [this answer](https://stackoverflow.com/a/17901248/15497888). – Henry Ecker Feb 27 '22 at 23:20

0 Answers0