Possible Duplicate:
Weighted random selection with and without replacement
我有一个长长的物品清单。我想根据概率从列表中随机选择一个项目。列表如下所示:
class Item:
def __init__(self, pid, hits, qtyPerOrder):
self.pid = pid
self.bay = hits
self.qtyPerOrder = int(qtyPerOrder)
itemList = [('RGSCAF', 181 ,6), ('WAR10227', 54 ,3), ('AD2020WOC', 31 ,4)]
基本上,我需要一个函数,它将遍历列表,根据命中率分配概率权重,然后根据概率随机选择n个对象。所以在这个例子中,返回对象('RGSCAF',181,6)的概率更高,因为它具有最高的命中值。在
不是最快的解决方案,但它能让人明白:
相关问题 更多 >
编程相关推荐