关于python:如何将两个字典合并为多个键值对

How can I merge two dictionaries with multiple key value pairs

我有两个听写,如下所示。我在python 2.7上。

1
2
3
4
5
6
7
8
9
10
11
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" }]

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

1
2
3
4
5
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" }]

我希望"条目"和"视图"键值对与它们最初使用的日期在同一数据段中。


由于dict条目似乎匹配,所以只需zip同时列出并用第二个dict更新一个dict,然后插入到列表中。

1
2
3
4
5
6
7
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)

结果:

1
[{'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'}]

它会更改第一个列表。如果您不想这样做,您必须在更新之前执行EDOCX1[1]

编辑:如本问题A所述,使用"dict addition"编辑一行:

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

在最简单的形式中,您迭代一个字典并在第二个字典中搜索相同的键。找到后,每天将第一个字典条目复制到一个新的字典,这样您的新字典将包含关键字"time"、"entries"及其值。然后用键"view"更新新的dict,它的值来自第二个字典视图"per_day"。现在,将它附加到列表区域图表数据

1
2
3
4
5
6
7
>>> 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)

结果:

1
2
3
4
5
6
>>> 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'}]


只需尝试使用zip并用dict-2更新dict-1

1
2
3
4
5
6
7
8
9
10
11
12
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

输出:

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

使用dict.update方法的"原始"解决方案:

1
2
3
4
5
6
7
8
9
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

输出:

1
[{'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'}]

您也可以使用单列理解:

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