dimanche 13 septembre 2015

How do I fill a list in python with elements from a specific set?

If I have a set of integers which denote the values that a list element can take and a python list of a given length.

I want to fill the list with all possible combinations.

example

list length=3 and the my_set ={1,-1}

Possible combinations

[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],
[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]

I tried approaching with random.sample method from random class but it doesn't help. I did:

my_set=[1,-1]
from random import sample as sm
print sm(my_set,1)    #Outputs: -1,-1,1,1 and so on..(random)
print sm(my_set,length_I_require)        #Outputs**:Error




Aucun commentaire:

Enregistrer un commentaire