简体   繁体   English

对键相同的列表中的字典值求和

[英]Sum the values of dictionaries inside a list where the keys are the same

I have 4 lists of dictionaries and each list contains 4 dictionaries.我有 4 个字典列表,每个列表包含 4 个字典。 The lists look similar to this:这些列表看起来类似于:

    A = [
     {'1': 150, '2': 160, '3': 140, '4': 110},
     {'1': 230, '2': 650, '3': 134, '4': 650},  
     {'1': 220, '2': 530, '3': 980, '4': 3450},  
     {'1': 150, '2': 160, '3': 440, '4': 110}]

    B = [
     {'1': 165, '2': 430, '3': 134, '4': 650},
     {'1': 64, '2': 650, '3': 345, '4': 340},  
     {'1': 220, '2': 650, '3': 340, '4': 134},  
     {'1': 150, '2': 160, '3': 234, '4': 2340}]

    C = [
     {'1': 678, '2': 430, '3': 134, '4': 650},
     {'1': 344, '2': 650, '3': 345, '4': 340},  
     {'1': 220, '2': 650, '3': 340, '4': 134},  
     {'1': 150, '2': 160, '3': 234, '4': 2340}]

    D = [
     {'1': 165, '2': 430, '3': 134, '4': 650},
     {'1': 64, '2': 650, '3': 345, '4': 340},  
     {'1': 220, '2': 650, '3': 340, '4': 134},  
     {'1': 150, '2': 160, '3': 234, '4': 1440}]

I would like to sum the values of the first dictionary of each list, where the keys are the same (so for key '1', then for '2', then for '3' and so on...), together with the values of the first dictionary of the rest of the lists.我想总结每个列表的第一个字典的值,其中键是相同的(所以对于键'1',然后对于'2',然后对于'3'等等...),连同列表 rest 的第一个字典的值。
Then do the same for the second, third and fourth dictionary.然后对第二个、第三个和第四个字典执行相同的操作。

The expected outcome should be 4 dictionaries, that maintain the order of the keys while summing up the values for the keys that are the same.预期的结果应该是 4 个字典,它们保持键的顺序,同时对相同键的值求和。

I have tried the piece of code below:我试过下面的代码:

for i in range (4):
    dict1 = A[i]
    dict2 = B[i]
    dict3 = C[i]
    dict4 = D[i]
# adding the values with common key      
    Cdict = Counter(dict1) + Counter(dict2) + Counter(dict3) + Counter(dict4)
    print(Cdict)

But the problem is that the order of the keys is changing:但问题是键的顺序正在改变:

Counter({'4': 2060, '2': 1450, '1': 1158, '3': 542})
Counter({'2': 2600, '4': 1670, '3': 1169, '1': 702})
Counter({'4': 3852, '2': 2480,'3': 2000, '1': 880})
Counter({'4': 6230, '3': 1142, '2': 640, '1': 600})

IIUC this is pretty straight forward. IIUC 这很简单。 You just write a function to sum up all values per key.您只需编写一个 function 来总结每个键的所有值。 Then apply it to the transposed list of lists of dicts.然后将其应用于字典列表的转置列表。

def sum_by_key(dicts):
    result = {}

    for d in dicts:
        for k, v in d.items():
            result[k] = result.get(k, 0) + v

    return result

lists_of_dicts = [[{1:2, 3:4}, {1:10, 2:9}], [{3:8, 2:4}, {3:1, 2:5}]]
result = [sum_by_key(dicts) for dicts in zip(*lists_of_dicts)]

print(result)

( lists_of_dicts would be [A, B, C, D] with your variables) lists_of_dicts将是[A, B, C, D]和你的变量)

Output: Output:

[{1: 2, 3: 12, 2: 4}, {1: 10, 2: 14, 3: 1}]

edit: with your new sample data编辑:使用您的新示例数据

lists_of_dicts = [A, B, C, D]
result = [sum_by_key(dicts) for dicts in zip(*lists_of_dicts)]
print(result)

produces产生

[{'1': 1158, '2': 1450, '3': 542, '4': 2060}, {'1': 702, '2': 2600, '3': 1169, '4': 1670}, {'1': 880, '2': 2480, '3': 2000, '4': 3852}, {'1': 600, '2': 640, '3': 1142, '4': 6230}]

I use numpy. And btw, I think the result you gave is incorrect, mine from the code below is:我使用 numpy。顺便说一句,我认为你给出的结果不正确,我的代码如下:

results:
{'1': 1158, '2': 1450, '3': 542, '4': 2060}
{'1': 702, '2': 2600, '3': 1169, '4': 1670}
{'1': 880, '2': 2480, '3': 2000, '4': 3852}
{'1': 600, '2': 640, '3': 1142, '4': 6230}

import numpy as np

A = [
    {'1': 150, '2': 160, '3': 140, '4': 110},
    {'1': 230, '2': 650, '3': 134, '4': 650},
    {'1': 220, '2': 530, '3': 980, '4': 3450},
    {'1': 150, '2': 160, '3': 440, '4': 110}]

B = [
    {'1': 165, '2': 430, '3': 134, '4': 650},
    {'1': 64, '2': 650, '3': 345, '4': 340},
    {'1': 220, '2': 650, '3': 340, '4': 134},
    {'1': 150, '2': 160, '3': 234, '4': 2340}]

C = [
    {'1': 678, '2': 430, '3': 134, '4': 650},
    {'1': 344, '2': 650, '3': 345, '4': 340},
    {'1': 220, '2': 650, '3': 340, '4': 134},
    {'1': 150, '2': 160, '3': 234, '4': 2340}]

D = [
    {'1': 165, '2': 430, '3': 134, '4': 650},
    {'1': 64, '2': 650, '3': 345, '4': 340},
    {'1': 220, '2': 650, '3': 340, '4': 134},
    {'1': 150, '2': 160, '3': 234, '4': 1440}]

group_list = [A,B,C,D]
items_num = len(A)
results =  []

for i in range(items_num):
    cur_item_dict = dict()

    for key in A[0].keys():
        cur_item_dict[key] = np.sum([ls[i][key]  for j, ls in enumerate(group_list)])

    results.append(cur_item_dict)

print('results:')
for res in results:
print(res)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM