Pet*_*ter 16

>>> import random
>>> thelist = ['a', 'b', 'c', 'd']
>>> random.shuffle(thelist)
>>> thelist
['d', 'a', 'c', 'b']
Run Code Online (Sandbox Code Playgroud)

你的结果(希望!)会有所不同.


Phi*_*hil 15

import random
random.shuffle(thelist)
Run Code Online (Sandbox Code Playgroud)

请注意,这会将列表原位洗牌.


Ric*_*dle 6

使用模块中的shuffle功能random:

>>> from random import shuffle
>>> thelist = ['a','b','c','d']
>>> shuffle(thelist)
>>> thelist
['c', 'a', 'b', 'd']
Run Code Online (Sandbox Code Playgroud)

  • 嘿,你怎么会得到彼得不同的输出?;) (10认同)