我很困惑在Python 3.6中使用新的random.choices
。
这是the doc:
random.choices(population, weights=None, *, cum_weights=None, k=1)
- 返回从替换人群中选择的k大小的元素列表。如果人口空无一人,就会引发
IndexError
。
他们举了一个例子:weights=[10, 5, 30, 5]
,我不知道这意味着什么。为什么他们总和不到100?如果我的人口是[1, 2, 3, 4]
- 这是否意味着选择'10'的概率为0.1?
总重量为10 + 5 + 30 + 5 = 50。
假设人口是[1,2,3,4]。
它以10/50 = 0.2的概率返回1
它返回2的概率为5/50 = 0.1
它以30/50 = 0.6的概率返回3
它以5/50 = 0.1的概率返回4