test_performance.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:xphyle 作者: jdidion 项目源码 文件源码
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.

    This function is borrowed from the python 3.6 'random' package.
    """
    if cum_weights is None:
        if weights is None:
            _int = int
            total = len(population)
            return [population[_int(random() * total)] for i in range(k)]
        cum_weights = list(accumulate(weights))
    elif weights is not None:
        raise TypeError('Cannot specify both weights and cumulative weights')
    if len(cum_weights) != len(population):
        raise ValueError('The number of weights does not match the population')
    total = cum_weights[-1]
    return [population[bisect(cum_weights, random() * total)] for i in range(k)]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号