python - How to shuffle a list 6 times? -
python - How to shuffle a list 6 times? -
i want shuffle list 6 times maintain getting same result 6 occasions. can help me find fault is?
here code used
import random lis1=[0,1,2,3] lis2=[] in range(6): random.shuffle(lis1) lis2.append(lis1) print lis2
and here sample result got
[[1,3,2,0],[1,3,2,0],[1,3,2,0],[1,3,2,0],[1,3,2,0],[1,3,2,0]]
if jumbled lists, how can sort them in ascending order? in,i want -
[[0,1,2,3],[2,3,1,0],[2,1,3,0],[1,0,3,2]]
into this-
[[0,1,2,3],[1,0,3,2],[2,1,3,0],[2,3,1,0]]
first, code repeatedly inserts lis1
reference lis2
. since lis1
stays same time, of lis2
elements end pointing same object. prepare this, need alter append()
line create re-create of list each time:
lis2.append(lis1[:])
now, sort result phone call sort()
after loop:
lis2.sort()
python list random-sample
Comments
Post a Comment