2016-10-28 119 views
-1

我有一個如下所示的字典列表。基於某個鍵/值合併/總結一系列字符

[ 
    {"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"} 
] 
+2

那你試試這麼遠嗎? – Tryph

+2

如果它們具有相同的日期值但type_of值不同,該怎麼辦?你保留哪一個? –

+0

@ Farhan.K我們可以假定type_of總是「foo」。 –

回答

0

更多與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謝謝,修復。 –

0

我建立類似一個你需要,希望它會幫助你完成你的任務,一些代碼。

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」不正確寫入)

1

任何理由不使用熊貓嗎? 這是非常方便,讓您的生活變得更輕鬆:

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}] 
+0

這個工程 - 現在弄清楚它是如何工作的。謝謝! –

+0

很好用。我在代碼中添加了一些註釋以使其更加明確。希望澄清! – FLab

+0

此外,我已經改變了總和的計算,所以它會工作,即使你有更多的類型在同一天 – FLab