Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to shuffle an array and display it in the corresponding order

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading