1

I want to turn A into B:

A = [0, 4, 1, 3, 2]
B = [0, 2, 4, 3, 1]

B represents turning the values of A into indices and the indices of A into values. How do I do this quickly with a numpy array A?

jdehesa
  • 58,456
  • 7
  • 77
  • 121
user3180
  • 1,369
  • 1
  • 21
  • 38
  • 4
    Possible duplicate of [How to invert a permutation array in numpy](https://stackoverflow.com/questions/11649577/how-to-invert-a-permutation-array-in-numpy) – jdehesa May 24 '19 at 12:51
  • From the duplicate question, pick the `invert_permutation` function at the end of [Ali's answer](https://stackoverflow.com/a/25535723/1782792) (that is, you do `import numpy as np; a = np.array([0, 4, 1, 3, 2]); b = np.empty_like(a); b[a] = np.arange(len(a)); print(b)` and you get `[0 2 4 3 1]`). – jdehesa May 24 '19 at 12:52
  • `A=np.array([0, 4, 1, 3, 2])` and then it's just `A[A]` – Brenlla May 24 '19 at 13:04

0 Answers0