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

3

我有两个字典如下所示。我正在使用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" }]

我希望“entries”和“views”的键值对与它们最初所在的日期位于同一个数据段中。

2
它们不是两个字典,而是两个列表,每个列表包含多个字典。 - Haris
1
你有一个尝试但是没有成功吗?如果是这样,请在问题中包含它以及它的输出(或者它生成的异常)。 - glibdud
字典列表是否总是有序的,就像示例中一样? - murphy
4个回答

2

既然字典条目似乎匹配,只需将两个列表进行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()

编辑:使用“字典加法”可以简化代码,参考这个问题的回答:

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

1
在最简单的形式中,您需要迭代一个字典并搜索第二个字典中相同的键。当找到时,将第一个字典的“entries_per_day”复制到一个新字典中,使您的新字典包含“time”、“entries”键及其值。然后使用第二个字典的“views_per_day”键和值更新新字典的“view”键。现在,将其附加到列表“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'}]

你的回答被标记为“低质量”。我添加了输出,以便它不会被删除,因为你是SO上的新手。请记得尽可能展示你的输出,并为未来使用这些页面的用户解释你的代码如何工作。 - Bill Bell

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

0

尝试使用 zip 并更新 dict-1 与 dict-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'}]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接