What is the most efficient way to sum a dict with multiple keys by one key?

落爺英雄遲暮 提交于 2019-12-02 07:18:02

Using basic Python, this doesn't get a whole lot better. You could hack something together with itertools.groupby, but it'd be ugly and probably slower, certainly less clear.

As @9769953 suggested, though, Pandas is a good package to handle this sort of structured, tabular data.

In [1]: import pandas as pd
In [2]: df = pd.DataFrame(lst)
Out[2]:
  Name  price  qty
0    A     10  100
1    A     10  100
2    A     10  100
3    B     10  100
4    C     10  100
5    C     10  100
In [3]: df.groupby('Name').agg(sum)
Out[3]:
      price  qty
Name
A        30  300
B        10  100
C        20  200

You just need a little extra mojo if you don't want to keep the data as a dataframe:

In [4]: grouped = df.groupby('Name', as_index=False).agg(sum)
In [5]: list(grouped.T.to_dict().values())
Out[5]:
[{'Name': 'A', 'price': 30, 'qty': 300},
 {'Name': 'B', 'price': 10, 'qty': 100},
 {'Name': 'C', 'price': 20, 'qty': 200}]

On the verbose side, but gets the job done:

group_lst = []
lst_of_names = []
for item in lst:
    qty_total = 0
    price_total = 0

    # Get names that have already been totalled
    lst_of_names = [item_get_name['Name'] for item_get_name in group_lst]

    if item['Name'] in lst_of_names:
        continue

    for item2 in lst:
        if item['Name'] == item2['Name']:
            qty_total += item2['qty']
            price_total += item2['price']

    group_lst.append(
        {
            'Name':item['Name'],
            'qty':qty_total,
            'price':price_total
        }
    )
pprint(group_lst)

Output:

[{'Name': 'A', 'price': 30, 'qty': 300},
 {'Name': 'B', 'price': 10, 'qty': 100},
 {'Name': 'C', 'price': 20, 'qty': 200}]

You can use defaultdict and Counter

>>> from collections import Counter, defaultdict
>>> cntr = defaultdict(Counter)
>>> for d in lst:
...     cntr[d['Name']].update(d)
...
>>> res = [dict(v, **{'Name':k}) for k,v in cntr.items()]
>>> pprint(res)
[{'Name': 'A', 'price': 30, 'qty': 300},
 {'Name': 'C', 'price': 20, 'qty': 200},
 {'Name': 'B', 'price': 10, 'qty': 100}]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!