jeudi 4 juin 2015

Having problems with python's random.shuffle

I would like to make a matrix whose lines are different permutations of the following vector=
[1,2,3,4,5,10,10,10,10,10]
I tried to use the random.shuffle function, but it seems that everytime I use that, It redefines all objects to which it is assigned. For example, I have tried the following:

vec=[1,2,3,4,5]  
for i in range(5):  
    vec.append(10)  
Matpreferences=[]  
for i in range(10):  
    random.shuffle(vec)  
    Matpreferences.append(vec)  

But then I get a matrix with all lines equal.
eg:

[0, 0, 1.0, 0, 2.0, 3.0, 0, 0.5, 2.5, 1.5, 0]    
[0, 0, 1.0, 0, 2.0, 3.0, 0, 0.5, 2.5, 1.5, 0]  
[0, 0, 1.0, 0, 2.0, 3.0, 0, 0.5, 2.5, 1.5, 0]  
[0, 0, 1.0, 0, 2.0, 3.0, 0, 0.5, 2.5, 1.5, 0]    
...

I even tried something like

Matpreferences=[]  
    for i in range(10):  
    random.shuffle(vec)
    a=vec
    Matpreferences.append(a)   

But it yields the same results. Is there any simple way to get around that?
Thanks a lot for any help given =)




Aucun commentaire:

Enregistrer un commentaire