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 to merge multiple mappings into a single mapping in Python

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to merge multiple mappings into a single mapping in Python. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

We have multiple dictionaries or mappings and want to logically merge them into a single mapping structure to perform specific operations, such as finding values or checking for the existence of keys.

Suppose there are two dictionaries:

A = {'x, respectively, 1, 2, 5, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 4.

Now suppose we perform a lookup operation, and we have to check these two dictionaries (for example, look in a first, or go to b if you don't find it). A simple way to solve this problem is to use the ChainMap class in the collections module. For example:

From collections import ChainMapa= {'x 'print (c [' z']) print (len (c) print (list (c.keys () print (list (c.values () a ['z'] = 5print (c ['z'])

Running result:

1233 ['yearly,' zonal,'x'] [2, 3, 1] 5

ChainMap can accept multiple mappings and logically they behave as a separate mapping mechanism. However, these mappings are not merged at run time. Instead, ChainMap simply maintains a list of underlying mappings, and then redefines common dictionary operations to scan the list. Most list operations work properly. For example: len, keys (), values ().

If there is a duplicate key, the corresponding value in the first mapping is taken.

The operation of modifying the mapping always acts on the first mapping structure listed. For example:

Del c ['x'] # can normally delete 'x':1del c [' y'] # in a, because there is no y key in the first mapping structure a

As an alternative to ChainMap, we might consider using the dictionary's update () method to merge multiple dictionaries together, for example:

In order to prevent b from being directly modified, cope a copy of bc=dict (b) print (id (c)) print (id (b)) c.update (a) print (c ['x']) print (c ['y']) print (c ['z'])

Running result:

45507694004549694808123

This works, but it requires building a complete dictionary object separately (or modifying one of them directly, but destroying the original data). In addition, if any of the original dictionaries are modified, the change will not be reflected in the merged dictionary, but ChainMap will do.

On how to merge multiple mappings into a single mapping in Python to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report