私は辞書とリストを使って以下のコードを書きました:
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()のインデックスが対応しているかどうかわからないため、エラーが発生する可能性があります。
これをもっと上手に書く方法を知っていますか?