按多个键分组并汇总/平均字典列表的值

编程入门 行业动态 更新时间:2024-10-11 13:19:58
本文介绍了按多个键分组并汇总/平均字典列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 Python 中按多个键分组和汇总/平均字典列表的最pythonic 方法是什么?假设我有一个字典列表,如下所示:

What is the most pythonic way to group by multiple keys and summarize/average values of a list of dictionaries in Python please? Say I have a list of dictionaries as below:

input = [ {'dept': '001', 'sku': 'foo', 'transId': 'uniqueId1', 'qty': 100}, {'dept': '001', 'sku': 'bar', 'transId': 'uniqueId2', 'qty': 200}, {'dept': '001', 'sku': 'foo', 'transId': 'uniqueId3', 'qty': 300}, {'dept': '002', 'sku': 'baz', 'transId': 'uniqueId4', 'qty': 400}, {'dept': '002', 'sku': 'baz', 'transId': 'uniqueId5', 'qty': 500}, {'dept': '002', 'sku': 'qux', 'transId': 'uniqueId6', 'qty': 600}, {'dept': '003', 'sku': 'foo', 'transId': 'uniqueId7', 'qty': 700} ]

所需的聚合输出:

output=[ {'dept': '001', 'sku': 'foo', 'qty': 400}, {'dept': '001', 'sku': 'bar', 'qty': 200}, {'dept': '002', 'sku': 'baz', 'qty': 900}, {'dept': '002', 'sku': 'qux', 'qty': 600}, {'dept': '003', 'sku': 'foo', 'qty': 700} ]

或平均值:

output=[ {'dept': '001', 'sku': 'foo', 'avg': 200}, {'dept': '001', 'sku': 'bar', 'avg': 200}, {'dept': '002', 'sku': 'baz', 'avg': 450}, {'dept': '002', 'sku': 'qux', 'avg': 600}, {'dept': '003', 'sku': 'foo', 'avg': 700} ]

我发现了这个:在 Python 中对字典列表的值进行分组和聚合 但它似乎没有给我想要的东西.

I have found this: Group by and aggregate the values of a list of dictionaries in Python but it doesn't seem to give me what I want.

推荐答案

获取汇总结果

from itertools import groupby from operator import itemgetter grouper = itemgetter("dept", "sku") result = [] for key, grp in groupby(sorted(input_data, key = grouper), grouper): temp_dict = dict(zip(["dept", "sku"], key)) temp_dict["qty"] = sum(item["qty"] for item in grp) result.append(temp_dict) from pprint import pprint pprint(result)

输出

[{'dept': '001', 'qty': 200, 'sku': 'bar'}, {'dept': '001', 'qty': 400, 'sku': 'foo'}, {'dept': '002', 'qty': 900, 'sku': 'baz'}, {'dept': '002', 'qty': 600, 'sku': 'qux'}, {'dept': '003', 'qty': 700, 'sku': 'foo'}]

为了得到平均值,你可以简单地改变 for 循环内的内容,就像这样

And to get the averages, you can simply change the contents inside the for loop, like this

temp_dict = dict(zip(["dept", "sku"], key)) temp_list = [item["qty"] for item in grp] temp_dict["avg"] = sum(temp_list) / len(temp_list) result.append(temp_dict)

输出

[{'avg': 200, 'dept': '001', 'sku': 'bar'}, {'avg': 200, 'dept': '001', 'sku': 'foo'}, {'avg': 450, 'dept': '002', 'sku': 'baz'}, {'avg': 600, 'dept': '002', 'sku': 'qux'}, {'avg': 700, 'dept': '003', 'sku': 'foo'}]

建议:无论如何,我会像这样在同一个 dict 中同时添加 qty 和 avg

Suggestion: Anyway, I would have added both the qty and avg in the same dict like this

temp_dict = dict(zip(["dept", "sku"], key)) temp_list = [item["qty"] for item in grp] temp_dict["qty"] = sum(temp_list) temp_dict["avg"] = temp_dict["qty"] / len(temp_list) result.append(temp_dict)

输出

[{'avg': 200, 'dept': '001', 'qty': 200, 'sku': 'bar'}, {'avg': 200, 'dept': '001', 'qty': 400, 'sku': 'foo'}, {'avg': 450, 'dept': '002', 'qty': 900, 'sku': 'baz'}, {'avg': 600, 'dept': '002', 'qty': 600, 'sku': 'qux'}, {'avg': 700, 'dept': '003', 'qty': 700, 'sku': 'foo'}]

更多推荐

按多个键分组并汇总/平均字典列表的值

本文发布于:2023-10-23 14:20:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字典   平均   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!