按多个键进行分组,并汇总/列出词典的平均值 [英] Group by multiple keys and summarize/average values of a list of dictionaries
问题描述
通过多个按键组合最多的pythonic方式是什么,并在Python中总结/列出词典的平均值?说我有一个字典列表如下:
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.
推荐答案
从运算符import itemgetter $ b获取聚合结果
To get the aggregated results
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'}]
为了获得平均值,您可以简单地更改内容side for 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'}]
建议:无论如何,我会添加 / code>和
avg
在同一个 dict
像这样
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'}]
这篇关于按多个键进行分组,并汇总/列出词典的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!