在python中拆分嵌套字典

时间:2018-04-23 22:49:30

标签: python dictionary

我有一个简单的事情,但由于某种原因,我的思想现在被阻止了 这类数据的名称是什么

[{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'}]

如何将它们分成2个词典

它也可以是动态的并改为

[{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'} ,{'self': 'tes_2', 'id': 6, 'name': 'IT Voice Support'} ]

我想要一个可以动态的解决方案

3 个答案:

答案 0 :(得分:1)

最简单的方法是:

dict1, dict2 = [{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'}]

将它们解压缩到变量dict1和dict2

您也可以直接索引到列表中,例如

dict_list = [{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'}]
dict1 = dict_list[0]
dict2 = dict_list[1]

答案 1 :(得分:0)

您可以通过在两个变量之间进行划分来划分它。

\'$'

变量' part1'将是" {'':'}"}"同时' part2'将是" {' z':' y'}"。

答案 2 :(得分:0)

你可以在python中解压缩迭代!

a, b = [{'self': 'test', 'id': 4, 'name': 'IT Network Support'}, {'self': 'tes_1', 'id': 5, 'name': 'IT PC Support'}]