如果我的总体中的项目数等于我想要的样本数,我会收到错误消息。
这是一个最小的例子
import random
subset = random.sample( set([312996, 529565, 312996, 130934]) , 4)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-b816cd5c3651> in <module>()
----> 1 subset = random.sample( set([312996, 529565, 312996, 130934]) , 4)
/opt/conda/lib/python3.6/random.py in sample(self, population, k)
318 n = len(population)
319 if not 0 <= k <= n:
--> 320 raise ValueError("Sample larger than population or is negative")
321 result = [None] * k
322 setsize = 21 # size of a small set minus size of an empty list
ValueError: Sample larger than population or is negative
编辑
似乎这只发生在这 4 个数字上。我试过了
import random
subset = random.sample( set([2, 5, 8, 9]) , 4)
我没有收到错误。我无法弄清楚第一个问题是什么。. .
相关分类