在一个列表中存储多个整数值并返回最佳值对

reo*_*eox 3 python dictionary

我有以下三个整数值:

id        # identifies the pair
entropy   # gives entropy information
len       # basicly the length of a string
Run Code Online (Sandbox Code Playgroud)

现在我想存储许多这些值,并选择总体上具有最高熵和长度值的前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
Run Code Online (Sandbox Code Playgroud)

这可以轻松完成吗?

jam*_*lak 5

在构建完这样的字典后,您可以获得前10个值.虽然如果你在构建字典时找到它们会有更有效的解决方案,如果可能的话.

import heapq
heapq.nlargest(10, (k for k in d if d[k][1] > n), key=lambda k: d[k][0])
Run Code Online (Sandbox Code Playgroud)