recipe-117241.py 文件源码

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

项目:code 作者: ActiveState 项目源码 文件源码
def windex(lst):
    '''an attempt to make a random.choose() function that makes weighted choices

    accepts a list of tuples with the item and probability as a pair
    like: >>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
    >>> y=windex(x)'''
    n = random.uniform(0, 1)
    for item, weight in lst:
        if n < weight:
            break
        n = n - weight
    return item
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号