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

Python:如何在字典中获取特定组的最大值?,python,dictionary,Python,Dictionary,因此,为了在中获得最大字典值,我可以执行以下操作: dict[max(dict, key=dict.get)] 我的字典是这样的: {文章标题:链接到它的不同文章的数量} 例如: {‘10世纪’:2,‘波兰’:0,‘墨西哥’:11} 从group()中,我得到一个元组列表(article_title,article object),例如: [(墨西哥()),(波兰())] 对于该组,我想检查文章标题的最大价值是多少 但我如何找到一组特定键的字典值呢? 我真的迷路了。。。我觉得我写的没有意义:

因此,为了在中获得最大字典值,我可以执行以下操作:

dict[max(dict, key=dict.get)]
我的字典是这样的: {文章标题:链接到它的不同文章的数量}

例如: {‘10世纪’:2,‘波兰’:0,‘墨西哥’:11}

从group()中,我得到一个元组列表(article_title,article object),例如: [(墨西哥()),(波兰())]

对于该组,我想检查文章标题的最大价值是多少

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

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\u title,article\u object)
的元组,您希望将
article\u title
作为键,我们可以做的是获取这些键的值,如
dict.get(title)for title,article in group()
,然后找到这些值中的最大值。因此,你的问题的答案是:

max(dict.get(title) for title, article in group())
小提示:
dict
不是变量的好名字,因为它隐藏了python的保留关键字
dict

按组,我假定您指的是密钥的子集

为了清楚起见,我带了一本每月的字典去查温度。
max_key
会告诉我温度最高的月份。
max\u group\u key
会告诉我该组中温度最高的月份

temperature = {
    "jan": 17, "feb": 18, "mar": 19, "apr": 24, "may": 26, 
    "jun": 25, "jul": 22, "aug": 21, "sep": 20, "oct": 20,
    "nov": 18, "dec": 15
}

# hottest month
max_key = max(temperature, key=temperature.get)
max_val = temperature[max_key]

print("hottest month: {0}, temperature: {1}".format(max_key, max_val))

# only for a few months
group = [ ("jan", "foo"), ("feb", "bar"), ("jun", "baz") ]
group = [ i[0] for i in group ]
max_group_key = max(group, key=temperature.get)
max_group_val = temperature[max_group_key]

print("hottest month: {0}, temperature: {1}".format(max_group_key, max_group_val))

请提供
group()
的定义,以及词典的示例和预期输出。
dict=my dictionary
隐藏
dict
关键字,或者至少如果它不包含空格,因此会引发一个
SyntaxError
我添加了一个示例,说明dictionary和group()输出应该是什么样子的。@Bun如果您将组转换为只包含字典的键,即“poland”、“mexico”,那么我认为我的答案是有效的
temperature = {
    "jan": 17, "feb": 18, "mar": 19, "apr": 24, "may": 26, 
    "jun": 25, "jul": 22, "aug": 21, "sep": 20, "oct": 20,
    "nov": 18, "dec": 15
}

# hottest month
max_key = max(temperature, key=temperature.get)
max_val = temperature[max_key]

print("hottest month: {0}, temperature: {1}".format(max_key, max_val))

# only for a few months
group = [ ("jan", "foo"), ("feb", "bar"), ("jun", "baz") ]
group = [ i[0] for i in group ]
max_group_key = max(group, key=temperature.get)
max_group_val = temperature[max_group_key]

print("hottest month: {0}, temperature: {1}".format(max_group_key, max_group_val))