Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

python - Consolidate list of dictionaries elements in dictionary

I have below complex structure with dictionaries with a number of repetitive keys. I want to consolidate the below structure and return the below structure. Tried multiple ways but not able to crack it. Please help.

Input structure:

{'lst_1': [{"127.0.0.1": [{“key1”:{“asm”: “entry1”, “CHG”: [“entry2”]}}]}, {"127.0.0.1": [{“key1”: {“PL”: “entry3”, “DU”: “entry4”}}]}, {"127.0.0.1": [{“key1”: {“SL”: “entry5”, “DU”: “entry6”}}]}], 'lst_0': [{"127.0.0.1": [{“key2”: {“asm”: “entry7”, “CHG”: [“entry8”]}}]}, {"127.0.0.1": [{“key2: {“PL”: “entry9”, “DU”: “entry10”}}]}, {"127.0.0.1": [{“key2”: {“SL”: “entry11”, “DU”: “entry12”}}]}]}

Output Structure:

{‘key1’: {‘asm’: ‘entry1’, 'SL': ‘entry5’, 'CHG’: [‘entry2’], 'DU': ‘entry6’, 'PL': ‘entry3’}, ‘key2’: {‘asm’: ‘entry7’, 'SL': ‘entry11’, 'CHG’: [‘entry8’], 'DU': ‘entry12’, 'PL': ‘entry9’}}
question from:https://stackoverflow.com/questions/65838511/consolidate-list-of-dictionaries-elements-in-dictionary

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This one does the job.

def filterDict(complex_dict):
    filtered_dict = {}

    for a in complex_dict.values():
        for b in a:
            for c in b.values():
                for d in c:
                    for e, f in d.items():
                        if e not in filtered_dict:
                            filtered_dict[e] = {}

                        filtered_dict[e].update(f)

    return filtered_dict

complex_dict = {'lst_0': [{'127.0.0.1': [{'key2': {'CHG': ['entry8'],
                                    'asm': 'entry7'}}]},
           {'127.0.0.1': [{'key2': {'DU': 'entry10',
                                    'PL': 'entry9'}}]},
           {'127.0.0.1': [{'key2': {'DU': 'entry12',
                                    'SL': 'entry11'}}]}],
 'lst_1': [{'127.0.0.1': [{'key1': {'CHG': ['entry2'],
                                    'asm': 'entry1'}}]},
           {'127.0.0.1': [{'key1': {'DU': 'entry4',
                                    'PL': 'entry3'}}]},
           {'127.0.0.1': [{'key1': {'DU': 'entry6',
                                    'SL': 'entry5'}}]}]}

print(filteredDict(complex_dict))

## OUTPUT:
# {'key1': {'CHG': ['entry2'],
#           'DU': 'entry6',
#           'PL': 'entry3',
#           'SL': 'entry5',
#           'asm': 'entry1'},
#  'key2': {'CHG': ['entry8'],
#           'DU': 'entry12',
#           'PL': 'entry9',
#           'SL': 'entry11',
#           'asm': 'entry7'}}

I guess there's nothing to explain here since this is just a bunch of nested loops to get the values needed for the filtered dictionary.

I'm quite curious why you have this kind of structure for your dictionary. If you can and able to, I suggest restructuring your dictionary, so you won't have to deal with this kind of nested loops.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...