The Shuffle () method randomly sorts all elements of a sequence.
Here's the syntax:
1 Import Random 2 3 random.shuffle (LST)
LST can be a sequence or a tuple;
1>>>ImportRandom;2>>> indexlist=[1,2,4,5,8,6];3>>>indexlist4[1, 2, 4, 5, 8, 6]5>>>random.shuffle (indexlist)6>>>indexlist7[1, 2, 4, 5, 8, 6]8>>>random.shuffle (indexlist)9>>>indexlistTen[6, 2, 4, 5, 1, 8] One>>>random.shuffle (indexlist) A>>>indexlist -[2, 1, 4, 5, 6, 8]
If it is a ' range ' type, an error is generated:
1>>> Indexlist=range (10);2>>>type (indexlist)3<class 'Range'>4>>>random.shuffle (indexlist)5 Traceback (most recent):6File"<pyshell#28>", Line 1,inch<module>7 random.shuffle (indexlist)8File"D:\softwareTool\Python\anzhuang_file\lib\random.py", line 272,inchShuffle9X[i], x[j] =X[j], X[i]TenTypeError:'Range'Object does notSupport Item Assignment
Shuffle functions in Python