mercredi 27 janvier 2021

why cant I keep my randomly generated elements in list? [closed]

I have the following function which generates 'n' random combinations of k tuples from a main list, list_1:

list_1=[(0, 1), (0, 2), (0, 5), (0, 10), (0, 20), (0, 50), (0, 100), (2, 1), (2, 2), (2, 5), (2, 10), (2, 20), (2, 50), (2, 100), (5, 1), (5, 2), (5, 5), (5, 10), (5, 20), (5, 50), (5, 100), (10, 1), (10, 2), (10, 5), (10, 10), (10, 20), (10, 50), (10, 100), (20, 1), (20, 2), (20, 5), (20, 10), (20, 20), (20, 50), (20, 100), (50, 1), (50, 2), (50, 5), (50, 10), (50, 20), (50, 50), (50, 100), (100, 1), (100, 2), (100, 5), (100, 10), (100, 20), (100, 50), (100, 100)]

rand_list=[]
def rand_gen(n):

    for i in range(n):
        i=random.choices(list_1, k=6)
        if i not in rand_list:
            rand_list.append(i)

rand_gen(3)

print(rand_list)

which gets the following output:

print(rand_list)
>>>[[(50, 2), (20, 5), (50, 20), (2, 1), (2, 100), (5, 2)], [(50, 20), (20, 1), (20, 50), (2, 20), (20, 100), (20, 20)], [(50, 20), (2, 5), (2, 100), (100, 50), (100, 2), (100, 10)]]

My issue is that when I run the function again to get another n lists of k tuples, I get a whole new list, instead of keeping the old values and appending the new ones, like so:

[[(100, 100), (100, 10), (0, 5), (0, 1), (0, 10), (50, 2)], [(0, 50), (100, 20), (2, 100), (20, 10), (100, 50), (0, 100)], [(0, 2), (0, 5), (20, 10), (2, 50), (5, 10), (0, 1)]]

As you can see its a new list and the ones originally generated have vanished.

Does anyone know how i can keep the original combinations for subsequent runs of the function, rather than just replacing them over and over?




Aucun commentaire:

Enregistrer un commentaire