将嵌套字典转换成字典 [英] Convert nested dictionary into a dictionary
本文介绍了将嵌套字典转换成字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的字典清单
[
{'id':1, 'name': 'name1', 'education':{'university':'university1', 'subject': 'abc1'}},
{'id':2, 'name': 'name2', 'education':{'university':'university2', 'subject': 'abc2'}},
{'id':3, 'name': 'name3', 'education':{'university':'university3', 'subject': 'abc3'}},
]
我想将其转换为
[
{'id':1, 'name': 'name1', 'university':'university1', 'subject': 'abc1'},
{'id':2, 'name': 'name2', 'university':'university2', 'subject': 'abc2'},
{'id':3, 'name': 'name3', 'university':'university3', 'subject': 'abc3'},
]
有没有什么pythonic的方法可以解决这个问题.
is there any pythonic way to solve this.
推荐答案
您可以简单地执行以下操作:
You could simply do the following:
l = [...]
for d in l:
d.update(d.pop('education', {}))
# l
[{'id': 1, 'name': 'name1', 'subject': 'abc1', 'university': 'university1'},
{'id': 2, 'name': 'name2', 'subject': 'abc2', 'university': 'university2'},
{'id': 3, 'name': 'name3', 'subject': 'abc3', 'university': 'university3'}]
这篇关于将嵌套字典转换成字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文