我有以下三個整數值:存儲多個整數值在一個列表,並返回最佳值對
id # identifies the pair
entropy # gives entropy information
len # basicly the length of a string
現在我想要存儲的許多設置值,選擇排名前10位總具有最高熵在n
from collections import defaultdict
d = defaultdict(list)
for id, entropy, len in generateValues:
d[id].append(entropy)
d[id].append(len)
# now get the top 10 values
的長度值可以這樣很容易做到?
爲什麼使用defaultdict?難道你不能只用'd [id] = [entropy,len]' – jamylak
@jamylak設置這個,google告訴我的第一件事是defaultdict ...我會認爲你的解決方案更具可讀性:) – reox