gpt4 book ai didi

Python:如何获取字典中特定组的最大值?

转载 作者:行者123 更新时间:2023-11-28 22:31:12 32 4
gpt4 key购买 nike

所以我found为了获得最大的字典值,我可以这样做:

dict[max(dict, key=dict.get)]

我的字典是这样的: {article_title:number of how many different articles linked to it}

例子: {'10 世纪':2,'波兰':0,'墨西哥':11}

从 group() 我得到一个元组列表(article_title,文章对象),例如: [(墨西哥, 墨西哥()), (波兰, 波兰())]

对于该组,我想检查 article_title 可能具有的最大值是多少。

但是如何找到特定键组的字典值呢?我真的迷路了……我觉得我写的没有意义:

dict = my dictionary
group_of_keys = group() # returns specific list of tuples. The first term of the tuple is my key, the second is irrelevant
max_value = dict[max(dict[group_of_keys], key=dict.get)]

请帮忙!

最佳答案

我假设 group() 返回一个键列表。如果是这样,您可以获得这些键的值,然后从中找出最大值。

max(map(dict.get, groups()))

编辑: 正如您阐明的那样,group() 返回一个 (article_title, article_object) 的元组,并且您想要 article_title as key 我们可以做的是获取这些键的值作为 dict.get(title) for title, article in group() 然后找到这些值中的最大值。所以,你的问题的答案是:

max(dict.get(title) for title, article in group())

小提示:dict 不是一个好的变量名,因为它隐藏了 python 的保留关键字 dict

关于Python:如何获取字典中特定组的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41701554/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com