Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Python merges a list of dictionaries

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how Python merges the list of dictionaries". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the list of how Python merges dictionaries.

Recently, I made a requirement for data consolidation, which is recorded here:

The requirements are as follows:

Dict_of_list1 = [{"name": "zhangsan", "age": 18}, {"name": "lisi", "age": 19}, {"name": "wangwu", "age": 20}] dict_of_list2 = [{"name": "zhangsan", "province": "shandong"}, {"name": "lisi" "province": "changsha", {"name": "xiaohong", "province": "guangxi"}, {"name": "wangwu", "province": "xizang"}]

Merge the dictionaries of the two lists above and put the items with the same name together.

It looks like:

[{"name": "zhangsan", "age": 18, "province": "shandong"}, {"name": "xiaohong", "province": "guangxi"}] traditional solution dict_of_list1 = [{"name": "zhangsan", "age": 18}, {"name": "lisi", "age": 19}, {"name": "wangwu", "age": 20}] dict_of_list2 = [{"name": "zhangsan" "province": "shandong"}, {"name": "lisi", "province": "changsha"}, {"name": "xiaohong", "province": "guangxi"}, {"name": "wangwu" "province": "xizang"}] all_data = dict_of_list1 + dict_of_list2d = {} for item in all_data: name = item ["name"] if name in d: d [name] .update (item) else: d [name] = itemresult = [] for k V in d.items (): result.append (v) sorted (v) print (result)

According to the request, we need to merge according to the same name, so we can create a new dictionary that can use the name as the key of the dictionary, and then determine whether the key is in the dictionary, and if it does not exist, it is an assignment. Otherwise, update its value. Because its value is a dictionary type, it can be updated through update, which can be applied to other languages.

Here is another solution that is unique to python.

By grouping functions

Using python standard library

From operator import itemgetterfrom itertools import groupbyfrom collections import ChainMap

Dict_of_list1 = [{"name": "zhangsan", "age": 18}, {"name": "lisi", "age": 19}, {"name": "wangwu", "age": 20}] dict_of_list2 = [{"name": "zhangsan", "province": "shandong"}, {"name": "lisi" "province": "changsha"}, {"name": "xiaohong", "province": "guangxi"}, {"name": "wangwu" "province": "xizang"}] key= itemgetter (name) all_data = dict_of_list1 + dict_of_list2all_data.sort (key=key) result_list = [] for x, y in groupby (all_data, key=key): d = dict (ChainMap (* y) result_list.append (d) sorted (result_list, key=key) print (result_list))

We can use the groupby of the itertools module to group by name

Groupby has two parameters, the first is an iterable object, and the second is to specify what to group by, similar to the value of key specified when we sort.

Here the value of key can be expressed using lambda, or the itemgetter method of the standard library operator can be used to achieve the same effect.

One requirement for using this method is that iterable objects need to be sorted in advance.

After groupby modification, we get a key and itertools._grouper object, key is the value of the key we specified, here is the value of name, and then we can split the itertools._grouper object to get several dictionaries, and then use the ChainMap of collections.

Its elements are dictionary merged and then converted to dict objects. Finally add to the designated list to complete our task.

The main purpose of this method is to be familiar with the usage of some practical standard libraries of Python.

Thank you for your reading, the above is the content of "how Python merges the list of dictionaries". After the study of this article, I believe you have a deeper understanding of the problem of how Python merges the list of dictionaries, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report