[英]how to choose a random 5 strings out of 10
I have one list consisting of 10 questions.我有一个包含 10 个问题的列表。 How do I choose 5 (max) at random and there can be no doubles - in python.
我如何随机选择 5(最大)并且不能有双打 - 在 python 中。
I have tried using:我试过使用:
from random import choice
strings = ['aa','bb','cc','dd','ee']
print(choice(strings))
But have no Idea how to choose 5 and have no doubles.但是不知道如何选择 5 并且没有双打。
The method random.choice()
draws samples with replacement . random.choice()
方法使用替换抽取样本。 What you want is drawing samples without replacment .您想要的是无需更换的抽样。 This is what
random.sample()
does.这就是
random.sample()
所做的。
Example with 5 samples drawn:抽取 5 个样本的示例:
print(random.sample(strings, 5))
See the documentation .请参阅文档。
This should work:这应该有效:
import random
random.sample(strings, 5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.