如何合并具有多个键值对的两个词典

时间:2017-02-23 13:58:10

标签: python python-2.7

我有两个dict,如下所示。我在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" }]   

如何将两个词典合并为第三个,以便输出如下所示:

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" }]

我想要"条目"和"观点"键值对与它们最初的日期位于同一数据段中。

4 个答案:

答案 0 :(得分:2)

由于dict条目似乎匹配,只有zip列出并用第二个dict更新一个dict,然后插入列表。

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中所述的“dict添加”的单行:

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

答案 1 :(得分:1)

以最简单的形式,您迭代一个字典并在第二个字典中搜索相同的密钥。找到后,将第一个字典 entries_per_day 复制到新的字典中,这样您的新字典就会包含按键' time','条目&# 39;和他们的价值观。然后使用键'查看'更新新的词典。它来自第二个字典 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'}]

答案 2 :(得分:0)

使用dict.update方法

“Raw”解决方案:

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']]

答案 3 :(得分:0)

尝试使用zip并使用dict-2

更新dict-1
name

输出:

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