是否可以重建此列表(结果片段):
[
[
{
"counthigh": 3,
"brgy_locat": "Barangay 2",
"municipali": "Cabadbaran City"
}
],
[
{
"brgy_locat": "Barangay 2",
"countmedium": 22,
"municipali": "Cabadbaran City"
}
],
[
{
"brgy_locat": "Barangay 2",
"countlow": 13,
"municipali": "Cabadbaran City"
}
]
]
这样的事情?
[
[
{
"counthigh": 3,
"countmedium": 22,
"countlow": 13,
"brgy_locat": "Barangay 2",
"municipali": "Cabadbaran City"
}
]
]
列表是我的查询的输出,我无法更改它。我正在使用Django。
编辑:假设我们有相同brgy_locat
和municipali
与counthigh: 5
的其他词典,它应该添加两个值。重建列表应为counthigh:8
。给出的结果只是一个片段。
答案 0 :(得分:4)
>>> from itertools import chain
>>> result = {}
>>> for d in chain.from_iterable(A):
... result.update(d)
...
>>> result
{'municipali': 'Cabadbaran City', 'counthigh': 3, 'brgy_locat': 'Barangay 2', 'countmedium': 22, 'countlow': 13}