简体   繁体   English

我如何从字典中获得最大的价值?

[英]How do i get the biggest value from a dictionary?

i still learning python3.我还在学习python3。 please help my problem:) i have a dictionary请帮助我的问题:) 我有一本字典

    "527740905101197317": {
        "lvl": 7,
        "exp": 6,
        "gems": 333,
        "items": {},
        "bank": 0
    },
    "600576767777832972": {
        "lvl": 6,
        "exp": 14,
        "gems": 100,
        "items": {},
        "bank": 0
    },
    "580843977352413185": {
        "lvl": 1,
        "exp": 700,
        "gems": 6765,
        "items": {},
        "bank": 0
    },
    "720726494640341161": {
        "lvl": 3,
        "exp": 2,
        "gems": 1234,
        "items": {},
        "bank": 0
    },
    "657959364933451796": {
        "lvl": 1,
        "exp": 480,
        "gems": 42,
        "items": {},
        "bank": 0
    },
    "724932280405065830": {
        "lvl": 1,
        "exp": 1,
        "gems": 1256,
        "items": {}
    },

how do i get the biggest "gems" with python3?我如何使用 python3 获得最大的“宝石”? i've tried some of tutorial, but none of it work.我已经尝试了一些教程,但都没有奏效。

Iterate over all the dictionaries using the .values() method, select the 'gems' value, and take the maximum.使用.values()方法遍历所有字典,select 是'gems'值,并取最大值。

max(d['gems'] for d in my_data.values())

I'd reccomend using a built-in max function, and specifying a key argument:我建议使用内置的max function,并指定一个key参数:

max_gems_key = max(dictionary, key=lambda a: dictionary[a]['gems'])
max_gems_val = dictionary[max_gems_key]

Let me simplify and break down everything:让我简化并分解所有内容:

def keyFunction(a):
    return dictionary[a]['gems']
max_gems_key = max(dictionary, key=keyFunction)
max_gems_val = dictionary[max_gems_key]

What is happening: I first create a function that finds gems when received the dictionary key - the gems value is what max would use to indentify what's larger, since you can't tell what's larger without it (eg is {'a':1,'b':2} > {'a':2,'b':1} ?).发生了什么:我首先创建了一个 function,它在收到字典键时会找到gems - gems值是max用来识别更大的值,因为没有它你无法分辨出更大的值(例如{'a':1,'b':2} > {'a':2,'b':1} ?)。 Then, I call the max function, and it iterates through all the keys, finding the biggest - it, again, uses keyFunc to determine how big a key is, and assignes the biggest key to max_gems_key .然后,我调用max function,它遍历所有键,找到最大的 - 它再次使用keyFunc确定键的大小,并将最大的键分配给max_gems_key A little further information about max usage here .有关max使用量更多信息。

Hope that's helpful!希望这会有所帮助!

The simplest way if you are a beginner is just to loop over the key,value pairs and keep track of the key for the largest one.如果您是初学者,最简单的方法就是遍历键、值对并跟踪最大的键。 Here is a reference to the docs about looping over dictionary items. 是对有关遍历字典项的文档的参考。

You have a nested dictionary, so you are going to loop over the outer dictionary and examine the entries in the inner dictionary to find the largest.您有一个嵌套字典,因此您将遍历外部字典并检查内部字典中的条目以找到最大的。

Something like this:像这样的东西:

def find_max(my_dict):
    key_for_max = None
    max_gem = -1
    # loop over outer dictionary
    for key, value in my_dict.items():
        # look into inner dictionary items to find maximum value for the 'gems' key
        if value['gems'] > max_gem:
            max_gem = value['gems']
            key_for_max = key

    return key_for_max, max_gem

I am assuming that you want the key for the entry that has the max 'gems' value, not just the value itself.我假设您想要具有最大“宝石”值的条目的键,而不仅仅是值本身。

Other alternatives are to sort it using the 'gems' value as a sorting key, but this is likely the simplest for you to follow as a newer programmer.其他替代方法是使用 'gems' 值作为排序键对其进行排序,但这对于新手程序员来说可能是最简单的方法。

As this is a nested dictionary if you want it using pandas you can try this way too.因为这是一个嵌套字典,如果你想使用pandas你也可以尝试这种方式。

import pandas as pd
df = pd.DataFrame(data_dict).T
df['gems'].max()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM