I need some help shuffling an array. Suppose I have the array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and this is a numpy array. I want to shuffle the array into a form that looks like this: [4, 3, 6, 5, 8, 7, 1, 9, 10, 2]
Then I want to make the array look like this. Basically, the first item in the previous list indicates that that item should be fourth in the list, so I would put 1 in the fourth position of a separate matrix, and so on. Is there a built in function to do this in python and how could I do this?
I have tried looking up a function that does this, but to no avail.
Any help would be appreciated.
>Solution :
Numpy’s fancy indexing was built for this:
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([4, 3, 6, 5, 8, 7, 1, 9, 10, 2])
n = np.zeros(10)
n[y-1] = x
print(n)
Ouptut:
[ 7. 10. 2. 1. 4. 3. 6. 5. 8. 9.]