-1

i still learning 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? i've tried some of tutorial, but none of it work.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Does this answer your question? [Python extract max value from nested dictionary](https://stackoverflow.com/questions/33915649/python-extract-max-value-from-nested-dictionary) – Mad Physicist Jun 25 '20 at 05:08

4 Answers4

4

Iterate over all the dictionaries using the .values() method, select the 'gems' value, and take the maximum.

max(d['gems'] for d in my_data.values())
Hans Musgrave
  • 6,613
  • 1
  • 18
  • 37
1

I'd reccomend using a built-in max function, and specifying a key argument:

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 (e.g. is {'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. A little further information about max usage here.

Hope that's helpful!

rizerphe
  • 1,340
  • 1
  • 14
  • 24
0

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.

Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18
0

As this is a nested dictionary if you want it using pandas you can try this way too.

import pandas as pd
df = pd.DataFrame(data_dict).T
df['gems'].max()
Akhilesh_IN
  • 1,217
  • 1
  • 13
  • 19