由于python 3.5中没有random.choices(),该函数是否有其他替代方法?
liuqian125 回答:在python 3.5中使用random.choices()的替代方法是什么
将代码从python 3.6复制到Mark Dickinson的建议
from itertools import accumulate as _accumulate,repeat as _repeat
from bisect import bisect as _bisect
import random
def choices(population,weights=None,*,cum_weights=None,k=1):
"""Return a k sized list of population elements chosen with replacement.
If the relative weights or cumulative weights are not specified,the selections are made with equal probability.
"""
n = len(population)
if cum_weights is None:
if weights is None:
_int = int
n += 0.0 # convert to float for a small speed improvement
return [population[_int(random.random() * n)] for i in _repeat(None,k)]
cum_weights = list(_accumulate(weights))
elif weights is not None:
raise TypeError('Cannot specify both weights and cumulative weights')
if len(cum_weights) != n:
raise ValueError('The number of weights does not match the population')
bisect = _bisect
total = cum_weights[-1] + 0.0 # convert to float
hi = n - 1
return [population[bisect(cum_weights,random.random() * total,hi)]
for i in _repeat(None,k)]
现在您可以安心使用choices
函数了!