我有一个大约有10000个项目的列表。当前情况是每个项目都有一个关联权重(优先级或重要性)。现在最小权重是
您知道如何编写一个
-100
(负值和零值可以删除),最高权重是 1500
。权重是由人们通过直觉确定的(某人认为该项目对社区的重要性如何)。因为确定最重要的项目并不容易,所以我想使用一些随机因素,这样权重较低的项目将有较少的选择机会,并且它们的权重将来会进行调整(混合常识和随机性)。您知道如何编写一个
getItem
函数吗?def getItem(dict):
# this function should return random item from
# the dictionary of item-weight pairs (or list of tuples)
# Normally I would return only random item from the dictionary,
# but now I'd like to have this: The item with weight 1500 should
# have much more chance to be returned than the item with weight 10.
# What's my idea is to sum up the weights of all items and then compute
# some ratios. But maybe you have better idea.
return randomItem
谢谢你