基于某个键/值合并/总结一系列字符
问题描述:
我有一个如下所示的字典列表。基于某个键/值合并/总结一系列字符
[
{"value1": 53, "day": "Thu, 07 May 2015", "value2": 70, "type_of": "foo"},
{"value1": 17, "day": "Thu, 07 May 2015", "value2": 12, "type_of": "foo"},
{"value1": 21, "day": "Thu, 12 May 2013", "value2": 40, "type_of": "foo"}
]
现在,我想这对在求和"value1"
/"value2"
键和离开单独"type_of"
的方式"day"
键相同的值类型的字典结合起来。
下面是我想要的结果:
[
{"value1": 70, "day": "Thu, 07 May 2015", "value2": 82, "type_of": "foo"},
{"value1": 21, "day": "Thu, 12 May 2013", "value2": 40, "type_of": "foo"}
]
答
更多与defaultdict紧凑的解决方案:
from collections import defaultdict
# data array in df
res = defaultdict (lambda : {"value1": 0, "value2": 0, "day": "", "type_of": "foo"})
for x in df:
res[x["day"]]["value1"] += x["value1"]
res[x["day"]]["value2"] += x["value2"]
res[x["day"]]["day"] = x["day"]
res[x["day"]]["type_of"] = x["type_of"]
print (res)
+0
此解决方案给我以下错误: TypeError:'datetime.date'对象没有属性'__getitem__' –
+0
@DidahDrieghe谢谢,修复。 –
答
我建立类似一个你需要,希望它会帮助你完成你的任务,一些代码。
list_dicts = [
{"value1": 53, "day": "Thu, 07 May 2015", "value2": 70, "type_of": "foo",},
{"value1": 17, "day": "Thu, 07 May 2015","value2": 12,"type_of": "foo"},
{"value1": 21, "day": "Thu, 12 May 2013", "value2": 40, "type_of": "foo"}
]
dicts_by_day = {}
for ldict in list_dicts:
if ldict["day"] in dicts_by_day:
dicts_by_day[ldict["day"]].append(ldict)
else:
dicts_by_day[ldict["day"]] = [ldict]
for day, values in dicts_by_day.items():
print(day,values)
在最后一次迭代中,您可以对每天的组进行操作。
尝试编辑您的问题(你的“list_dicts”不正确写入)
答
任何理由不使用熊猫吗? 这是非常方便,让您的生活变得更轻松:
import pandas as pd
l1 = [{"value1": 53, "day": "Thu, 07 May 2015", "value2": 70, "type_of": "foo"},
{"value1": 17, "day": "Thu, 07 May 2015","value2": 12,"type_of" : "foo"},
{"value1": 21, "day": "Thu, 12 May 2013", "value2": 40, "type_of": "foo"}]
# Create a "table" with keys as column names and set as index the pair (day, type_of)
df1 = pd.DataFrame(l1).set_index(['day', 'type_of'])
# Here you sum values with same day and type_of. This is already what you are
# looking for
out_df = df1.groupby(level=[0, 1]).sum()
# This last bit is to convert back to dictionary, although I would suggest to
# use pandas dataframes rather than lists of dictionaries
out_dict = out_df.reset_index().to_dict(orient = 'record')
# out = [{'day': 'Thu, 07 May 2015', 'value1': 70L, 'value2': 82L},
# {'day': 'Thu, 12 May 2013', 'value1': 21L, 'value2': 40L}]
那你试试这么远吗? – Tryph
如果它们具有相同的日期值但type_of值不同,该怎么办?你保留哪一个? –
@ Farhan.K我们可以假定type_of总是“foo”。 –