Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:获取字典列表中某个键的值之和_Python_List_Dictionary - Fatal编程技术网

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}

到目前为止你都试了些什么?你能看看我做的编辑吗。