本文介绍了按多个键分组并汇总/平均字典列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 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'}]更多推荐
按多个键分组并汇总/平均字典列表的值
发布评论