按多个键分组并汇总/平均词典列表的多个值 [英] Group by multiple keys and summarize/average multiple values of a list of dictionaries

查看:101
本文介绍了按多个键分组并汇总/平均词典列表的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,下面的代码遇到了问题.

I am new to Python and have ran into an issue with the below code.

我一直在寻找一种方法,可以按多个键进行分组并汇总/平均Python字典列表的值.以下代码(也位于此处的上一个问题/响应中:

I was looking for a way to group by multiple keys and summarize/average values of a list of dictionaries in Python. The below code (also located from previous question/response located here: Group by multiple keys and summarize/average values of a list of dictionaries) set me off on the right track but I am running into issues adding more field aggregation in the loop.

说我有一个字典列表,如下所示:

Say I have a list of dictionaries as seen below:

input = [
{'msn': '001', 'source': 'foo', 'status': '1', 'qty': 100, 'vol': 100},
{'msn': '001', 'source': 'bar', 'status': '2', 'qty': 200, 'vol': 200},
{'msn': '001', 'source': 'foo', 'status': '1', 'qty': 300, 'vol': 300},
{'msn': '002', 'source': 'baz', 'status': '2', 'qty': 400, 'vol': 100},
{'msn': '002', 'source': 'baz', 'status': '1', 'qty': 500, 'vol': 400},
{'msn': '002', 'source': 'qux', 'status': '1', 'qty': 600, 'vol': 100},
{'msn': '003', 'source': 'foo', 'status': '2', 'qty': 700, 'vol': 200}]

到目前为止,我的代码:

My code so far:

for key, grp in groupby(sorted(dict_list, key = grouper), grouper):
    temp_dict = dict(zip(["msn", "source"], key))
    temp_dict["qty"] = sum(item["qty"] for item in grp)
    temp_dict["vol"] = sum(item["vol"] for item in grp)
    result.append(temp_dict)

预期结果是:

{'msn': '001', 'source': 'foo', 'qty': 400, 'vol': 400},
{'msn': '001', 'source': 'bar', 'qty': 200, 'vol': 200},
{'msn': '002', 'source': 'baz', 'qty': 200, 'vol': 500},
{'msn': '003', 'source': 'foo', 'qty': 900, 'vol': 200}]

temp_dict["vol"] = sum(item["vol"] for item in grp)在for循环中的放置不会产生所需的结果,这最终是我的问题.

Placement of temp_dict["vol"] = sum(item["vol"] for item in grp) within the for loop does not produce the desired results which is ultimately my issue.

在添加(附加)另一个字段及其计算值到列表中时,如何保留键,如代码中所示分组?

How do I go about keeping the key, grouping as seen in the code while adding(appending) another field and its calculated value to the list?

在此先感谢您的帮助.

推荐答案

如果要多次遍历它,则需要复制" grpitertools.tee可以为您完成

You need to "copy" grp if you want to iterate through it multiple times, itertools.tee can do that for you

for key, grp in groupby(sorted(dict_list, key = grouper), grouper):
    temp_dict = dict(zip(["msn", "source"], key))
    grp1, grp2 = tee(grp)
    temp_dict["qty"] = sum(item["qty"] for item in grp1)
    temp_dict["vol"] = sum(item["vol"] for item in grp2)
    result.append(temp_dict)

这篇关于按多个键分组并汇总/平均词典列表的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆