如何在python中获取字典列表中的最大值

如何在python中获取字典列表中的最大值,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我有一个字典列表,如下所示,有不同的键和值 lst = [{'a': 15554}, {'v': 453}, {'a': 441742}, {'vb': 7785}, {'vv': 4275}, {'g': 7822}, {'l': 47537}, {'fg': 1144441565}] 我想使用python查找包含最高值的词典。 前任: fg:1144441565 谁能给我一个密码吗 提前感谢。您可以使用函数max: max(list1, key=lambda x: li

我有一个字典列表,如下所示,有不同的键和值

lst = [{'a': 15554}, {'v': 453}, {'a': 441742}, {'vb': 7785}, 
         {'vv': 4275}, {'g': 7822}, {'l': 47537}, {'fg': 1144441565}]
我想使用python查找包含最高值的词典。 前任:
fg:1144441565

谁能给我一个密码吗


提前感谢。

您可以使用函数
max

max(list1, key=lambda x: list(x.values()))
# {'fg': 1144441565}

您可以使用函数
max

max(list1, key=lambda x: list(x.values()))
# {'fg': 1144441565}

只需在字典列表中循环并每次检查值即可:

# Define the list
list1 = [{'a': 15554}, {'v': 453}, {'a': 441742}, {'vb': 7785}, 
{'vv': 4275}, {'g': 7822}, {'l': 47537}, {'fg': 1144441565}]

# Init variable to hold the maximum value and its corresponding key. 
# We just initialize it as the value of the first element:
max_val_key = list1[0].keys()[0]
max_val = list1[0][max_val_key]

# Loop through it using the 'in' operator
for dict in list1:
    key = dict.keys()[0]
    val = dict[key]

    # Check if current val is higher than the last defined max_val
    if (val > max_val):
        max_val = val
        max_val_key = key

只需在字典列表中循环并每次检查值即可:

# Define the list
list1 = [{'a': 15554}, {'v': 453}, {'a': 441742}, {'vb': 7785}, 
{'vv': 4275}, {'g': 7822}, {'l': 47537}, {'fg': 1144441565}]

# Init variable to hold the maximum value and its corresponding key. 
# We just initialize it as the value of the first element:
max_val_key = list1[0].keys()[0]
max_val = list1[0][max_val_key]

# Loop through it using the 'in' operator
for dict in list1:
    key = dict.keys()[0]
    val = dict[key]

    # Check if current val is higher than the last defined max_val
    if (val > max_val):
        max_val = val
        max_val_key = key

它可能的重复可以归结为:如何在不知道键的情况下从dict中获取第一个也是唯一的值。一旦你弄明白了这一点,你只需在
max(list1,key=…)
中使用它作为键函数。它可能的重复可以归结为:如何在不知道键的情况下从dict中获取第一个也是唯一的值。一旦你弄明白了这一点,你只需在
max(list1,key=…)
中使用它作为键函数。