mardi 2 août 2016

I want to randomly select items from a list and add them to another list without replacement

I'm trying to randomly select items from a list and add them to another list.

The list of elements I'm choosing from looks like this:

data=[2,3,4,7,8,12,17,24,27,33,35,36,37,38,40,43,44,50,51,54]

I want to randomly take an element from this list and add it to one of four lists until each list has the same number of elements.

lists=[[1,'x','x','x','x','x'],[3,'x','x','x','x','x'],[5,'x','x','x','x','x'],[7,'x','x','x','x','x']]

I have tried using random.choice but this gives me duplicates:

def fill_lists(data):
for list in lists:
    for n,i in enumerate(list):
        if i=='x':
            list[n]= random.choice(data)

I want my function to return a list that contains 4 lists each containing a random sample of the data list with no duplicates. I also want the first element of each list to be a value that I have already placed into the list.




Aucun commentaire:

Enregistrer un commentaire