📜  Python – 在嵌套字典中交换层次结构

📅  最后修改于: 2022-05-13 01:55:47.961000             🧑  作者: Mango

Python – 在嵌套字典中交换层次结构

有时,在使用Python字典时,我们可能会遇到需要执行嵌套字典的层次结构交换的问题。这个问题可以应用于需要字典重组的领域。让我们讨论可以执行此任务的某些方式。

方法 #1:使用循环 + items()
上述功能的组合可以用来解决这个问题。在此,我们迭代字典并使用蛮力重组。 items() 用于提取字典的所有键值对。

Python3
# Python3 code to demonstrate working of
# Swapping Hierarchy in Nested Dictionaries
# Using loop + items()
 
# initializing dictionary
test_dict = {'Gfg': { 'a' : [1, 3], 'b' : [3, 6], 'c' : [6, 7, 8]},
             'Best': { 'a' : [7, 9], 'b' : [5, 3, 2], 'd' : [0, 1, 0]}}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Swapping Hierarchy in Nested Dictionaries
# Using loop + items()
res = dict()
for key, val in test_dict.items():
    for key_in, val_in in val.items():
        if key_in not in res:
            temp = dict()
        else:
            temp = res[key_in]
        temp[key] = val_in
        res[key_in] = temp
 
# printing result
print("The rearranged dictionary : " + str(res))


Python3
# Python3 code to demonstrate working of
# Swapping Hierarchy in Nested Dictionaries
# Using  defaultdict() + loop
from collections import defaultdict
 
# initializing dictionary
test_dict = {'Gfg': { 'a' : [1, 3], 'b' : [3, 6], 'c' : [6, 7, 8]},
             'Best': { 'a' : [7, 9], 'b' : [5, 3, 2], 'd' : [0, 1, 0]}}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Swapping Hierarchy in Nested Dictionaries
# Using  defaultdict() + loop
res = defaultdict(dict)
for key, val in test_dict.items():
    for key_in, val_in in val.items():
        res[key_in][key] = val_in
 
# printing result
print("The rearranged dictionary : " + str(dict(res)))


输出 :

原字典:{'Gfg': {'a': [1, 3], 'c': [6, 7, 8], 'b': [3, 6]}, 'Best': {'d ': [0, 1, 0], 'a': [7, 9], 'b': [5, 3, 2]}}
重新排列的字典:{'d': {'Best': [0, 1, 0]}, 'a': {'Gfg': [1, 3], 'Best': [7, 9]}, ' c': {'Gfg': [6, 7, 8]}, 'b': {'Gfg': [3, 6], '最佳': [5, 3, 2]}}


方法 #2:使用 defaultdict() + 循环
这是解决此问题的另一种蛮力方法。在此,我们通过使用 defaultdict() 而不是传统的字典来减少一个关键的测试步骤。

Python3

# Python3 code to demonstrate working of
# Swapping Hierarchy in Nested Dictionaries
# Using  defaultdict() + loop
from collections import defaultdict
 
# initializing dictionary
test_dict = {'Gfg': { 'a' : [1, 3], 'b' : [3, 6], 'c' : [6, 7, 8]},
             'Best': { 'a' : [7, 9], 'b' : [5, 3, 2], 'd' : [0, 1, 0]}}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Swapping Hierarchy in Nested Dictionaries
# Using  defaultdict() + loop
res = defaultdict(dict)
for key, val in test_dict.items():
    for key_in, val_in in val.items():
        res[key_in][key] = val_in
 
# printing result
print("The rearranged dictionary : " + str(dict(res)))
输出 :

原字典:{'Gfg': {'a': [1, 3], 'c': [6, 7, 8], 'b': [3, 6]}, 'Best': {'d ': [0, 1, 0], 'a': [7, 9], 'b': [5, 3, 2]}}
重新排列的字典:{'d': {'Best': [0, 1, 0]}, 'a': {'Gfg': [1, 3], 'Best': [7, 9]}, ' c': {'Gfg': [6, 7, 8]}, 'b': {'Gfg': [3, 6], '最佳': [5, 3, 2]}}