jeudi 3 août 2023

How to assign array values to the n-1 location in the array

I have an array like this: [1, 2, 3, 4, 5]. I shuffle it to get this: [1, 5, 4, 2, 3].

The Goal: Assign each value in the array to the n-1 location in the array

In other words, I need the number 1 to be in 1st position, 2 in 5th, 3 in 4th, 2 in 5th, and 3 in 6th.

For example, the output of this one would be: [1, 4, 5, 3, 2].

Is there a built in function for this or a simple code that can accomplish this goal?

What I've done so far:

import numpy as np
import random
array = np.arange(1,6)
print(array)
random.shuffle(array)
print(array)



Aucun commentaire:

Enregistrer un commentaire