例如,我想使用嵌套字典并反转值
input = { "a" : { "x": 2, "y": 3 },
"b" : { "x": 5, "z": 7 } }
output = {'y': {'a': 3},
'x': {'a': 2, 'b': 5},
'z': {'b': 7} }
我有的:
def reverse_nest_dicts(nested_dict):
reverse_nest_dict = {}
for k, v in nested_dict:
for k2, v2 in nested_dict.values():
reverse_nest_dict[k2][k] = v2
return reverse_nest_dict