英文:
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(<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}}
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
,您觉得怎么样?
>>> from collections import Counter, defaultdict
>>> my_dict = defaultdict({...})
>>> flattened = {f'{K},{k}':v for K, d in my_dict.items() for k, v in d.items()}
>>> 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
?
>>> from collections import Counter, defaultdict
>>> my_dict = defaultdict({...})
>>> flattened = {f'{K},{k}':v for K, d in my_dict.items() for k, v in d.items()}
>>> Counter(flattened).most_common(3)
[('B,c', 4), ('B,a', 3), ('A,a', 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"{k1},{k2} : {v2}") for k1, v1 in dico.items() # dico is your d.dict
for k2, v2 in v1.items()], key=lambda x: -int(x.split(" : ")[1]))
Output :
print(*out[:N], sep="\n")
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'{key1},{key2}: {value}')
答案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 ={'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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论