合并字典中的字典元素列表
Consolidate list of dictionaries elements in dictionary
我有以下复杂的结构,其中包含带有许多重复键的字典。我想合并以下结构和 return 以下结构。尝试了多种方法,但无法破解。请帮忙
输入结构:
{'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”}}]}]}
输出结构:
{‘key1’: {‘asm’: ‘entry1’, 'SL': ‘entry5’, 'CHG’: [‘entry2’], 'DU': ‘entry6’, 'PL': ‘entry3’}, ‘key2’: {‘asm’: ‘entry7’, 'SL': ‘entry11’, 'CHG’: [‘entry8’], 'DU': ‘entry12’, 'PL': ‘entry9’}}
这个就可以了。
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'}}
我想这里没什么好解释的,因为这只是一堆嵌套循环来获取过滤字典所需的值。
我很好奇为什么你的字典有这种结构。如果可以的话,我建议你重组你的字典,这样你就不必处理这种嵌套循环了。
我有以下复杂的结构,其中包含带有许多重复键的字典。我想合并以下结构和 return 以下结构。尝试了多种方法,但无法破解。请帮忙
输入结构:
{'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”}}]}]}
输出结构:
{‘key1’: {‘asm’: ‘entry1’, 'SL': ‘entry5’, 'CHG’: [‘entry2’], 'DU': ‘entry6’, 'PL': ‘entry3’}, ‘key2’: {‘asm’: ‘entry7’, 'SL': ‘entry11’, 'CHG’: [‘entry8’], 'DU': ‘entry12’, 'PL': ‘entry9’}}
这个就可以了。
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'}}
我想这里没什么好解释的,因为这只是一堆嵌套循环来获取过滤字典所需的值。
我很好奇为什么你的字典有这种结构。如果可以的话,我建议你重组你的字典,这样你就不必处理这种嵌套循环了。