2017-02-23 86 views
0

我有兩個詞典如下所示。我在Python 2.7。如何合併兩個詞典與多個鍵值對

entries_per_day = [ {"time": "October 1", "entries": "5" }, 
       {"time": "October 2", "entries": "3" }, 
       {"time": "October 3", "entries": "1" }, 
       {"time": "October 4", "entries": "0" }, 
       {"time": "October 5", "entries": "23" }] 

views_per_day = [ {"time": "October 1", "views": "9" }, 
       {"time": "October 2", "views": "3" }, 
       {"time": "October 3", "views": "5" }, 
       {"time": "October 4", "views": "6" }, 
       {"time": "October 5", "views": "32" }] 

我如何合併兩個庫到第3,使輸出看起來像這樣:

area_chart_data = [ {"time": "October 1", "entries": "5", "views": "9" }, 
       {"time": "October 2", "entries": "3", "views": "3" }, 
       {"time": "October 3", "entries": "1", "views": "5" }, 
       {"time": "October 4", "entries": "0", "views": "6" }, 
       {"time": "October 5", "entries": "23", "views": "32" }] 

我想要的「項」和「意見」的鍵值對是在與最初使用日期相同的數據段。

+2

他們不是2個字典,他們是2個列表,每個列表包含多個字典。 – Haris

+1

你有沒有工作的嘗試?如果是這樣,請將其與其輸出(或其生成的例外)一起包含在問題中。 – glibdud

+0

是總是排序的字典列表,如示例中所示? – murphy

回答

1

由於字典條目似乎匹配,只需zip列出並更新一個字典與第二個字典,然後插入列表。

area_chart_data = [] 

for e,v in zip(entries_per_day,views_per_day): 
    e.update(v) 
    area_chart_data.append(e) 

print(area_chart_data) 

結果:

[{'views': '9', 'time': 'October 1', 'entries': '5'}, {'views': '3', 'time': 'October 2', 'entries': '3'}, {'views': '5', 'time': 'October 3', 'entries': '1'}, {'views': '6', 'time': 'October 4', 'entries': '0'}, {'views': '32', 'time': 'October 5', 'entries': '23'}] 

它改變了第一個列表。如果你不希望出現這種情況,你必須更新之前做e = e.copy()

編輯:使用一個班輪在this Q&A指出,「字典加法」:

area_chart_data = [dict(e, **v) for e,v in zip(entries_per_day,views_per_day)] 
0

「原始」使用dict.update解決方案方法:

area_chart_data = [] 
for entry in entries_per_day: 
    for view in views_per_day: 
     if entry['time'] == view['time']: 
      d = entry.copy() 
      d.update(view) 
      area_chart_data.append(d) 

print area_chart_data 

輸出:

[{'time': 'October 1', 'views': '9', 'entries': '5'}, {'time': 'October 2', 'views': '3', 'entries': '3'}, {'time': 'October 3', 'views': '5', 'entries': '1'}, {'time': 'October 4', 'views': '6', 'entries': '0'}, {'time': 'October 5', 'views': '32', 'entries': '23'}] 

你也可以使用一個單獨的列表理解:

area_chart_data = [dict(entry, **view) for entry in entries_per_day 
        for view in views_per_day if entry['time'] == view['time']] 
1

在最簡單的形式,你遍歷一個字典和搜索第二字典相同的密鑰。當找到時,將第一個字典entries_per_day複製到一個新的字典,所以你的新字典將包含鍵'時間','條目'和它們的值。然後用關鍵字'view'更新新的字典,它的值來自第二個字典views_per_day。現在,它追加到列表area_chart_data

>>> area_chart_data = [] 
>>> for d in entries_per_day: 
...  for f in views_per_day: 
...   if d["time"] == f["time"] : 
...    m = dict(d) 
...    m["views"] = f["views"] 
...    area_chart_data.append(m) 

結果:

>>> area_chart_data 
[{'time': 'October 1', 'entries': '5', 'views': '9'}, 
{'time': 'October 2', 'entries': '3', 'views': '3'}, 
{'time': 'October 3', 'entries': '1', 'views': '5'}, 
{'time': 'October 4', 'entries': '0', 'views': '6'}, 
{'time': 'October 5', 'entries': '23', 'views': '32'}] 
+0

您的回答標記爲「低質量」。我添加了輸出到它,所以它不會被刪除,因爲你是新手。請記得在可能的情況下顯示您的輸出結果,並解釋您的代碼如何爲未來這些頁面的使用帶來益處。 –

+0

謝謝@BillBell ...請記住這一點:) –

0

只是嘗試用ZIP和更新字典-1字典-2

lst1 = [ {"time": "October 1", "entries": "5" }, 
     {"time": "October 2", "entries": "3" }, 
     ] 

lst2 = [ {"time": "October 1", "views": "9" }, 
     {"time": "October 2", "views": "3" }, ] 


for x,y in zip(lst1,lst2): 
    x.update(y) 

print lst1 

輸出:

[{'views': '9', 'entries': '5', 'time': 'October 1'}, {'views': '3', 'entries': '3', 'time': 'October 2'}]