简体   繁体   English

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

[英]How can I merge two dictionaries with multiple key value pairs

I have two dict as shown below. 我有两个字典,如下所示。 I am on Python 2.7. 我使用的是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" }]   

How can I merger the two dictionaries into a 3rd so that the output looks like this: 如何将两个字典合并为第三个字典,以使输出看起来像这样:

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

I want the "entries" and "views" key-value pairs to be in the same data segment as the date they were originally with. 我希望“条目”和“视图”键值对与它们最初使用的日期位于同一数据段中。

Since the dict entries seem to match, just zip both lists and update one dict with the second one, then insert in a list. 由于字典条目似乎匹配,因此只需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)

result: 结果:

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

it changes the first list. 它更改了第一个列表。 If you don't want that, you have to do e = e.copy() before the update 如果您不希望这样做,则必须在更新之前执行e = e.copy()

EDIT: one-liner using "dict addition" as stated in this Q&A : 编辑:如本问答中所述,使用“ dict加法”的单线:

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

In the most simpler form, you iterate over one dictionary and search the same key in second dictionary. 以最简单的形式,您可以遍历一本字典并在第二本字典中搜索相同的键。 When found, copy the first dictionary entries_per_day to a new dict, so your new dictionary would contain the keys 'time', 'entries' and their values. 找到后,将第一个词典entry_per_day复制到新字典,这样您的新词典将包含键“ time”,“ entries”及其值。 Then update the new dict with the key 'view' and it's value from the second dictionary views_per_day . 然后用键“ view”更新新字典,它的值来自第二个字典views_per_day Now, append it to the list area_chart_data 现在,将其附加到列表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)

Result: 结果:

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

"Raw" solution using dict.update method: 使用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

The output: 输出:

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

You may also use a single list comprehension: 您还可以使用单个列表理解:

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

Just try to use zip and update dict-1 with dict-2 只需尝试使用zip并用dict-2更新dict-1

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

Output : 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM