简体   繁体   English

如何通过使用公共密钥对值进行求和,从一系列dicts创建单个Python dict?

[英]How to create single Python dict from a list of dicts by summing values with common keys?

I have a list of dictionaries, eg: 我有一个词典列表,例如:

dictList = [
    {'a':3, 'b':9, 'c':4},
    {'a':9, 'b':24, 'c':99},
    {'a':10, 'b':23, 'c':88}
]

All the dictionaries have the same keys eg a , b , c . 所有词典都具有相同的键,例如abc I wish to create a single dictionary with the same keys where the values are the sums of the values with the same keys from all the dictionaries in the original list. 我希望创建一个具有相同键的单个字典,其中值是原始列表中所有字典中具有相同键的值的总和。

So for the above example, the output should be: 所以对于上面的例子,输出应该是:

{'a':22, 'b':56, 'c':191}

What would be the most efficient way of doing this? 这样做最有效的方法是什么? I currently have: 我目前有:

result = {}
for myDict in dictList:
    for k in myDict:
        result[k] = result.setdefault(k, 0) + myDict[k]

If all dicts have all keys, you could do this as: 如果所有的词都有所有键,你可以这样做:

>>> dict((key, sum(d[key] for d in dictList)) for key in dictList[0])
{'a': 22, 'b': 56, 'c': 191}

[Edit] If speed is a big priority, you can also shave off ~20% (though at the cost of some readability) with the following instead: [编辑]如果速度是一个重要的优先事项,你也可以用以下代码减少~20%(虽然以一些可读性为代价):

import operator, itertools
dict((key, sum(itertools.imap(operator.itemgetter(key), dictList))) 
      for key in dictList[0])

The speed depends on the size of the dict. 速度取决于字典的大小。 I get the following timings for the original 3 item list, and for various different sizes (created by mutliplying the original list by 10, 100 or 1000 etc): 我得到了原始3项列表的以下时间,以及各种不同的大小(通过将原始列表多出10,100或1000等创建):

List Size   Original      dict+generator       imap+itemgetter
      3      0.054          0.090                0.097
     30      0.473          0.255                0.236
    300      4.668          1.884                1.529
   3000     46.668         17.975               14.499

(All times for 10,000 runs) (10,000次运行的所有时间)

So it's slightly slower for just 3, but two to three times as fast for larger lists. 所以它只有3个稍慢,但对于较大的列表来说要慢两到三倍。

Try this. 试试这个。

from collections import defaultdict
result = defaultdict(int)
for myDict in dictList:
    for k in myDict:
        result[k] += myDict[k]

I'm not sure how it relates to the other answers speed wise, but there is always 我不确定它与速度明智的其他答案有什么关系,但总有

from collections import Counter
result = sum(map(Counter,dictList),Counter())

Counter is a subclass of dict and it can be used in place of dict in most places. Counterdict的子类,它可以在大多数地方用来代替dict If necessary, you could just convert it back into a dict 如有必要,您可以将其转换回dict

result = dict(result)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字典列表合并到单个字典中,为公共键添加值 - Merge a list of dicts into a single dict adding values for common keys 如何在Python中组合多个字典,求和公共键的值(并保留值为0的键)? - How to combine multiple dicts, summing the values of common keys (and retaining those with value 0) in Python? Python 以列表为值的字典,使用原始键值的所有可能组合创建字典列表 - Python dict with lists as values, create list of dicts with all possible combinations of original keys-values 如何合并字典列表,对重复键的值求和 - How to merge a list of dicts, summing values for repeated keys 如何将具有公共键值对的字典列表转换为以键为公共值的新字典? - How do i convert the list of dicts having common key value pair to a new dict with keys as the common value? python-如何从字典列表中确定找到的两个键是否属于同一字典 - python - how to determine if two keys found belong to same dict from list of dicts 如何在字典列表和字典的嵌套字典中获取所有键和值? - how to get all keys&values in nested dict of list-of-dicts and dicts? 如何从列表中创建一个字典,其中值是列表的元素,键是python中这些元素的函数? - How to create a dict from list where values are elements of the list and keys are function of those elements in python? 从具有 2 个公共键的字典列表中构建新字典的最有效方法? - Most efficient way to build a new dict from a list of dicts with 2 common keys? 如何从Python中带有嵌套字典的字典中创建新的字典 - How do I create a new dict of dicts from a dict with nested dicts in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM