gpt4 book ai didi

python - 同时对多个项目的字典列表中的两个键求和

转载 作者:行者123 更新时间:2023-12-01 09:12:02 24 4
gpt4 key购买 nike

我想在一个函数中对两个不同的变量求和,但我希望根据多个其他项目对它们进行求和。

如果我有以下字典 x 列表:

x=[{'id':1, 'var1':'a', 'var2':'left', 'var3':0.1, 'var4':1},
{'id':2, 'var1':'a', 'var2':'right', 'var3':0.1, 'var4':1},
{'id':2, 'var1':'a', 'var2':'right', 'var3':0.2, 'var4':3},
{'id':4, 'var1':'b', 'var2':'left', 'var3':0.4, 'var4':4},
{'id':5, 'var1':'b', 'var2':'right', 'var3':0.1, 'var4':5},
{'id':5, 'var1':'b', 'var2':'right', 'var3':0.4, 'var4':2}]

然后我可以使用以下函数根据其他两个变量(“var1”和“var2”)对单个变量(“var3”)求和:

from operator import itemgetter
from itertools import groupby

def aggregate_var3_by_var1_and_var2(data):
my_data= []
grouper = itemgetter("id", "var1", "var2")
for key, grp in groupby(sorted(data, key = grouper), grouper):
temp_dict = dict(zip(["id", "var1", "var2"], key))
temp_dict["var3"] = sum(item["var3"] for item in grp)
my_data.append(temp_dict)
return my_data

my_output = aggregate_var3_by_var1_and_var2(x)

但是,我想对多个类别(“var1”和“var2”)中的多个变量(“var3”和“var4”)求和,以便输出如下所示:

 y=[{'id': 1, 'var1': 'a', 'var2': 'left', 'var3': 0.1, 'var4': 1},
{'id': 2, 'var1': 'a', 'var2': 'right', 'var3': 0.3, 'var4':4},
{'id': 4, 'var1': 'b', 'var2': 'left', 'var3': 0.4, 'var4':4},
{'id': 5, 'var1': 'b', 'var2': 'right', 'var3': 0.5, 'var4':7}]

如何使用此方法一次对多个变量求和?

最佳答案

您的方法的直接扩展,提供石斑鱼键和值键作为参数:

from operator import itemgetter
from itertools import groupby
from itertools import chain

def reducer(ts):
return map(sum, zip(*ts))

def agg(data, keys, aggfields):
my_data = []
getter = itemgetter(*aggfields)
grouper = itemgetter(*keys)
for ks, grp in groupby(sorted(data, key=grouper), grouper):
vs = map(getter, grp)
kvs = chain(zip(keys,ks), zip(aggfields, reducer(vs)))
my_data.append(dict(kvs))
return my_data

在回复中:

In [9]: x=[{'id':1, 'var1':'a', 'var2':'left', 'var3':0.1, 'var4':1},
...: {'id':2, 'var1':'a', 'var2':'right', 'var3':0.1, 'var4':1},
...: {'id':2, 'var1':'a', 'var2':'right', 'var3':0.2, 'var4':3},
...: {'id':4, 'var1':'b', 'var2':'left', 'var3':0.4, 'var4':4},
...: {'id':5, 'var1':'b', 'var2':'right', 'var3':0.1, 'var4':5},
...: {'id':5, 'var1':'b', 'var2':'right', 'var3':0.4, 'var4':2}]

In [10]: agg(x, ['var1','var2'], ['var3','var4'])
Out[10]:
[{'var1': 'a', 'var2': 'left', 'var3': 0.1, 'var4': 1},
{'var1': 'a', 'var2': 'right', 'var3': 0.30000000000000004, 'var4': 4},
{'var1': 'b', 'var2': 'left', 'var3': 0.4, 'var4': 4},
{'var1': 'b', 'var2': 'right', 'var3': 0.5, 'var4': 7}]

这是一种使用字典进行分组的替代方法(Counter dicts 的默认字典...)

from collections import Counter, defaultdict
from itertools import chain
from operator import itemgetter

def agg(data, keys, aggfields):

grouper = defaultdict(Counter)
pluck_keys = itemgetter(*keys)
pluck_vals = itemgetter(*aggfields)

for d in data:
ctr = grouper[pluck_keys(d)]
for k, v in zip(aggfields, pluck_vals(d)):
ctr[k] += v

return [
{k:v for k,v in chain(zip(keys, ks), ctr.items())}
for ks, ctr in grouper.items()
]

关于python - 同时对多个项目的字典列表中的两个键求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com