Select random item with weight

前端 未结 5 1763
执笔经年
执笔经年 2020-12-15 00:48

I have a list of approx. 10000 items. The current situation is that every item has an associated weight (priority or importance). Now the smallest weight is -100

5条回答
  •  轮回少年
    2020-12-15 01:11

    Python 3.6 introduced random.choices()

    def get_item(items, items_weights):
        return random.choices(items, weights=items_weights)[0]
    

提交回复
热议问题