vendredi 1 décembre 2017

How do I make sure that after shuffling a list, all values have actually changed index? (Python3.6)

The aim of my script is to create a list of progressive numbers:

examplelst=list(range(5))

Then, after importing random and shuffle, I want to randomise the list:

shuffle(examplelst)

Then I need to check if a value is still in its starting position, example:

before shuffling: [0,1,2,3,4]
after shuffling: [3,0,2*,4,1]

In this case, the value "2" still has index 2. Should this happen, I want to shuffle again until I get every index changed randomly. My approach so far:

done=False
shuffle(examplelst)

while not done:
    for i in examplelst:
        if examplelst[i]==int(i):
            shuffle(examplelst)
            print ('shuffled')
        else:
            done=True
if done == True:
    continue with my stuff knowing that the list is properly shuffled

I thought this would work but I'm wrong. Anyone able to help?




Aucun commentaire:

Enregistrer un commentaire