代码点读机 / Python / random.shuffle

random.shuffle Python · 模块功能

把列表顺序随机打乱。

怎么用

import random
random.shuffle(lst)

小例子

import random
lst = [1, 2, 3]
random.seed(0)
random.shuffle(lst)
print(lst)

运行结果

[1, 3, 2]

提醒:直接修改原列表。

官方文档:https://docs.python.org/zh-cn/3/library/random.html#random.shuffle

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条