嵌套词典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}
posted @ 2020-04-03 16:12  happy_fan  阅读(416)  评论(0编辑  收藏  举报