我使用字典和列表编写了以下代码:
d = computeRanks() # dictionary of id : interestRank pairs
lst = list(d) # tuples (id, interestRank)
interestingIds = []
for i in range(20): # choice randomly 20 highly ranked ids
choice = randomWeightedChoice(d.values()) # returns random index from list
interestingIds.append(lst[choice][0])
似乎可能存在错误,因为我不确定lst和d.values()中的索引之间是否存在对应关系。
你知道如何更好地写这个吗?