嵌套词典nested_dict
逐层访问词典,直到最后一层
from nested_dict import nested_dict
a = {"hi":{"you":{"dd": 3}}, "h":{"yo":{"d": 2}}}
for i in nested_dict(a).items_flat():
print(i)
输出
(('hi', 'you', 'dd'), 3)
(('h', 'yo', 'd'), 2)
b = {'.'.join(k): v for k, v in nested_dict(a).items_flat()}
print(b)
输出
{'hi.you.dd': 3, 'h.yo.d': 2}