python:获取字典列表中某个键的值之和
我有一份字典清单 例如:python:获取字典列表中某个键的值之和,python,list,dictionary,Python,List,Dictionary,我有一份字典清单 例如: l = [{"a" : 1}, {"b" : 5}, {"a" : 10}, {"b" : 10}, {"a" : 10} ] 我想这样得到每个键的和 { "a" : 30, "b" : 15 } 使用计数器 >>> from collections import Counter >>> c=Counter() >>> for d in l: ...
l = [{"a" : 1},
{"b" : 5},
{"a" : 10},
{"b" : 10},
{"a" : 10}
]
我想这样得到每个键的和
{
"a" : 30,
"b" : 15
}
使用
计数器
>>> from collections import Counter
>>> c=Counter()
>>> for d in l:
... c.update(d)
...
>>> dict(c)
{'a': 30, 'b': 15}
到目前为止你都试了些什么?你能看看我做的编辑吗。