随机选取序列中的一个元素。
>>> from random import choice
>>> lst = [1, 2, 3, 4]
>>> choice(lst)
3
随机选取序列中的多个元素(可重复)。k
值指定数量。
>>> from random import choices
>>> lst = [1, 2, 3, 4]
>>> choices(lst, k=3)
[4, 3, 4]
随机选取序列中的多个元素(不重复)。k
值指定数量。
>>> from random import sample
>>> lst = [1, 2, 3, 4]
>>> sample(lst, k=3)
[4, 3, 2]