Home > Blockchain >  Finding the max dict value of a 3-level nested dict
Finding the max dict value of a 3-level nested dict

Time:07-26

I have a 3-level nested dict and I would like to find the max value of the 3rd-level dict and have it mapped to the 2nd-level key it belongs to. For example, In "Loc-1" dict value, there are two 2nd-level keys that are "36", I want this key mapped to the value "56".

This is the dict:

{
    "Loc-1": {
        "A-1": {"36" : {"value" : "34"}},
        "A-2": {"36" : {"value" : "56"}},
        "A-3": {"48" : {"value" : "72"}},
        "A-4": {"100" : {"value" : "77"}},
        "A-5": {"48" : {"value" : "2"}},
        "A-6": {"100" : {"value" : "10"}},
        "A-7": {"44" : {"value": "21"}}
        
    }
    
    "Loc-2": {
        "A-8": {"44" : {"value" : "52"}},
        "A-9": {"48" : {"value" : "23"}},
        "A-10": {"40" : {"value" : "62"}},
        "A-11": {"153" : {"value" : "43"}},
        "A-12": {"40" : {"value" : "22"}},
        "A-13": {"153" : {"value" : "10"}},
        "A-14": {"36" : {"value": "21"}}
        
    }

}

This is the desired state:

{ 
    "Loc-1": {   
        "36" : "56",
        "48" : "72",
        "100": "77",
        "44" : "21"
        
    }
    
    "Loc-2": { 
        "36" : "21",
        "40" : "62",
        "48" : "23",
        "44" : "52",
        "153": "43",
        
    }
    
}

I'm finding it hard to compare one value to all the other values with the same key when it nested like this. How can I accomplish this?

CodePudding user response:

Nested loops will work:

results = {}

# 1st level: "Loc-1", "Loc-2"
for key1 in data:
    # Initialize to empty dictionary
    results[key1] = {}
    # 2nd level: A-1, A-2, etc
    for key2 in data[key1]:
        # First key 3rd level: 38, 36, etc
        key3 = next(iter(data[key1][key2]))
        value = data[key1][key2][key3]["value"]
        # Set max if already in results or just add
        if key3 in results[key1]:
            results[key1][key3] = max(results[key1][key3], value)
        else:
            results[key1][key3] = value

CodePudding user response:

IIUC, you can do the following.

def process(d):
    def process_(dic_1st_level):
        ' Get max values from 2nd level dics '
        result = {}
        for a, d_2nd_level in dic_1st_level.items():
            for label, v in d_2nd_level.items():
                result[label] = max(result.get(label, 0), v['value'], key = int)

        # Sort result by key as ints
        return dict(sorted(result.items(), key = lambda kv: int(kv[0])))

    return {k.replace("Loc", "Site"):process_(v) for k, v in d.items()}

Usage

print(process(d))   # d is the input nested dictionary

Output

{
    "Site-1": {
        "36": "56",
        "44": "21",
        "48": "72",
        "100": "77"
    },
    "Site-2": {
        "36": "21",
        "40": "62",
        "44": "52",
        "48": "23",
        "153": "43"
    }
}

CodePudding user response:

Let's start with the easy, inner-most dictionary first and work our way out to the outer dictionary. I will call the inner most dictionary simply dict_object, e.g. {"36" : {"value" : "34"}}. In order to work with this dictionary, I find it easier to transform it into a simpler structure: A tuple with two elements: ("36", "34"). For that, I created a function called transform_value:

def transform_value(dict_object):
    """Transform {"36" : {"value" : "34"}} --> ("36", "34")."""
    for key, value in dict_object.items():
        return key, value["value"]

# Test it out
>>> transform_value({"36" : {"value" : "34"}})
('36', '34')

Next, I also need to compare two strings numerically and return the larger one:

def maxint(a, b):
    """Take max of two strings which represents two ints."""
    return str(max(int(a), int(b)))

# Test it out:
>>> maxint("34", "56")
'56'

Working out (or up) one more level, the level with the 'Loc-1' values:

def transform_loc(loc_values):
    transformed = {}
    for dict_object in loc_values:
        key, value = transform_value(dict_object)
        transformed.setdefault(key, value)
        transformed[key] = maxint(transformed[key], value)
    return transformed

# Test it out, let's call the original structure `data`
>>> transform_loc(data["Loc-1"].values())
{'36': '56', '48': '72', '100': '77', '44': '21'}

Putting it all together:

new_data = {
    key: transform_loc(loc.values())
    for key, loc in data.items()
}

# New data is
{
    "Loc-1": {
        "36": "56",
        "48": "72",
        "100": "77",
        "44": "21"
    },
    "Loc-2": {
        "44": "52",
        "48": "23",
        "40": "62",
        "153": "43",
        "36": "21"
    }
}
  • Related