random.sample
时间: 2024-06-17 13:06:52 浏览: 9
random.sample是Python中的一个函数,用于从指定的序列中随机选择指定数量的元素,且不会重复。它的语法如下:
```python
random.sample(sequence, k)
```
其中,sequence是指定的序列,可以是列表、元组、字符串等可迭代对象;k是指定的元素数量。
下面是一个例子:
```python
import random
lst = [1, 2, 3, 4, 5]
result = random.sample(lst, 3)
print(result)
```
运行结果可能是:[2, 5, 1]。在这个例子中,我们从列表lst中随机选择了3个元素,且没有重复。