如何在Python中获取嵌套的defaultdict中的三个最大值?

huangapple go评论65阅读模式
英文:

How to get 3 greatest values in a nestet defaultdict in python?

问题

我有一个像这样的嵌套defaultdict:

defaultdict(<function __main__.<lambda>()>,
           {'A': defaultdict(<function __main__.<lambda>()>,
                 {'a': 2,
                  'b': 1,
                  'd': 2,
                  'f': 1}),
            'B': defaultdict(<function __main__.<lambda>()>,
                 {'a': 3,
                  'c': 4,
                  'e': 1})})

我想要得到这样的输出:

B,c : 4
B,a : 3
A,a : 2

我该如何对这样的嵌套defaultdict进行排序?

谢谢你的帮助。

英文:

I have a nested defaultdict like this:

defaultdict(&lt;function __main__.&lt;lambda&gt;()&gt;,
           {&#39;A&#39;: defaultdict(&lt;function __main__.&lt;lambda&gt;()&gt;,
                 {&#39;a&#39;:2,
                  &#39;b&#39;:1,
                  &#39;d&#39;:2,
                  &#39;f&#39;:1}
            &#39;B&#39;: defaultdict(&lt;function __main__.&lt;lambda&gt;()&gt;,
                 {&#39;a&#39;:3,
                  &#39;c&#39;:4,
                  &#39;e&#39;:1}}

I want to get an output like this:

B,c : 4
B,a : 3
A,a : 2

How can I sort a nested defaultdict like this?

Thanks for your help in advance.

答案1

得分: 1

关于将默认字典展平为 Counter,然后调用 Counter.most_common,您觉得怎么样?

&gt;&gt;&gt; from collections import Counter, defaultdict
&gt;&gt;&gt; my_dict = defaultdict({...}) 
&gt;&gt;&gt; flattened = {f'{K},{k}':v for K, d in my_dict.items() for k, v in d.items()}
&gt;&gt;&gt; Counter(flattened).most_common(3)
[('B,c', 4), ('B,a', 3), ('A,a', 2)]
英文:

How about flattening the default dict as a Counter, then calling Counter.most_common?

&gt;&gt;&gt; from collections import Counter, defaultdict
&gt;&gt;&gt; my_dict = defaultdict({...}) 
&gt;&gt;&gt; flattened = {f&#39;{K},{k}&#39;:v for K, d in my_dict.items() for k, v in d.items()}
&gt;&gt;&gt; Counter(flattened).most_common(3)
[(&#39;B,c&#39;, 4), (&#39;B,a&#39;, 3), (&#39;A,a&#39;, 2)]

答案2

得分: 1

以下是您要翻译的内容:

您可以尝试类似这样的代码

N = 3 # 调整此处的值以显示/打印前 N 个最大值

out = sorted([(f"{k1},{k2} : {v2}") for k1, v1 in dico.items() # dico 是您的字典
                      for k2, v2 in v1.items()], key=lambda x: -int(x.split(" : ")[1]))

输出

print(*out[:N], sep="\n")

B,c : 4
B,a : 3
A,a : 2

注意:已将 HTML 实体代码转换为普通文本。

英文:

You can try something like this :

N = 3 # adjust the value here to show/print the N greatest
​
out = sorted([(f&quot;{k1},{k2} : {v2}&quot;) for k1, v1 in dico.items() # dico is your d.dict
                      for k2, v2 in v1.items()], key=lambda x: -int(x.split(&quot; : &quot;)[1]))

Output :

print(*out[:N], sep=&quot;\n&quot;)
​
B,c : 4
B,a : 3
A,a : 2

答案3

得分: 0

我会以不同的方式处理。heapq.nlargest 跟踪 n 个最大的元素,而无需对它们进行排序。不幸的是,它并不会

import heapq
from operator import itemgetter

items = ((key1, key2, value)
         for key1, d in my_dict.items()
         for key2, value in d.items())
for key1, key2, value in heapq.nlargest(3, items, itemgetter(2)):
   print(f'{key1},{key2}: {value}')
英文:

I would go about it differently. heapq.nlargest keeps track of the n largest elements with the need to sort them. Unfortunately, it doesn't

import heapq
from operator import itemgetter

items = ((key1, key2, value)
         for key1, d in my_dict.items()
         for key2, value in d.items())
for key1, key2, value in heapq.nlargest(3, items, itemgetter(2)):
   print(f&#39;{key1},{key2}: {value}&#39;)

答案4

得分: 0

Here is the translated code portion:

将数据转换为具有数字作为其第一个项目的元组列表然后根据此数据按降序输出结果

*注意对于这个问题普通字典或defaultdict都将表现相同*

D ={'A': {'a':2,
          'b':1,
          'd':2,
          'f':1},
    'B': {'a':3,
          'c':4,
          'e':1}}

S = ((n,(k1,k2)) for k1,n2 in D.items() for k2,n in n2.items())
for n,k in sorted(S,reverse=True):
    print(",".join(k),":",n)

B,c : 4
B,a : 3
A,d : 2
A,a : 2
B,e : 1
A,f : 1
A,b : 1

如果您只需要前3个值您可以在排序输出上使用下标(`sorted(S,reverse=True)[:3]`)或者如其他人建议的那样使用heapq.nlargest
英文:

Convert the data to a list of tuples with the number as its first item. Then output the result based on this data sorted in descending order:

Note: a normal dictionary or a defaultdict will behave the same for this

D ={&#39;A&#39;: {&#39;a&#39;:2,
          &#39;b&#39;:1,
          &#39;d&#39;:2,
          &#39;f&#39;:1},
    &#39;B&#39;: {&#39;a&#39;:3,
          &#39;c&#39;:4,
          &#39;e&#39;:1}}

S = ((n,(k1,k2)) for k1,n2 in D.items() for k2,n in n2.items())
for n,k in sorted(S,reverse=True):
    print(&quot;,&quot;.join(k),&quot;:&quot;,n)

B,c : 4
B,a : 3
A,d : 2
A,a : 2
B,e : 1
A,f : 1
A,b : 1

If you only need the first 3 values, you can use a subscript on the sorted output (sorted(S,reverse=True)[:3]) or, as others suggested, use heapq.nlargest.

huangapple
  • 本文由 发表于 2023年4月17日 13:32:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定