1
2
3
4
5
6
import random
a = [1,2,3,4,5,6,7,8,9,10,11]
print(random.choice(a)) # 随机取1个
print(random.choices(a, k=3)) # 随机取3个,元素可能重复
print(random.sample(a, k=3)) # 随机取3个,元素不重复
输出
1
2
3
10
[8, 8, 5]
[9, 7, 10]
1
2
3
4
5
6
import random
a = [1,2,3,4,5,6,7,8,9,10,11]
print(random.choice(a)) # 随机取1个
print(random.choices(a, k=3)) # 随机取3个,元素可能重复
print(random.sample(a, k=3)) # 随机取3个,元素不重复
输出
1
2
3
10
[8, 8, 5]
[9, 7, 10]