【发布时间】:2012-02-13 11:49:48
【问题描述】:
我有一个大约的列表。 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
谢谢
【问题讨论】:
-
嗨,我最近回答了一个这样的问题:stackoverflow.com/a/9073313/862629