I want to create a function that makes a random permutation of a given list. I have made the following, but it gives me only 1 element of the list. If L = ["A", "B", "C"] the function will only print out ['A'], ['B'] or ['C']. But that's not what i want it to do, I want the function to print e.g. ['B', 'C', 'A'] instead of ['A'], ['B'] or ['C']. With other words, I want the while-loop to keep going until the list L is empty and have inserted all elements randomly at Lnew.
from random import randint
def permute(L):
Lnew = []
index = randint(0, len(L))
while index >= 0:
Lnew.insert(index, L[index])
return Lnew
I know that there are builtin functions as numpy.random.permutations and random.shuffle that I can use, but I want to create it from scratch, so I know how it works.
Thanks in Advance
Aucun commentaire:
Enregistrer un commentaire