The plurality of maps into a single map

More dictionaries or maps, it is desirable to logically combining them into a single map to perform certain operations, such as finding the value, or checks if the key exists.
If there are two dictionary objects a, b.

a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}

If now a check whether the key exists in which a dictionary object. A first check, if a is not present, then checking b. ChainMap module may be used collections classes, as follows:

from collections import ChainMap
c = ChainMap(a, b)
print(c['x'])   # Outputs 1 (from a)
print(c['y'])   # Outputs 2 (from b)
print(c['z'])   # Outputs 3 (from a)

ChainMap using a plurality of maps make them appear as a logically mapped. However, the mapping is not literally merge together. Instead, ChainMap keep a list of mapping a bottom, and operable to redefine commonly used dictionaries scan list. Most operations can be:

>>> len(c)
3
>>> list(c.keys())
['x', 'y', 'z']
>>> list(c.values())
[1, 2, 3]
>>>

If there are duplicate key, only the ChainMap acquired object instance corresponding to the key value of the first dictionary. Since? 'Z' have appeared in the a and b but acquiring c [ 'z'], the resulting is only 3, 4 instead.

Modify ChainMap object affects only its first dictionary of the relevant data. as follows:

>>> c['z'] = 10
>>> c['w'] = 40
>>> del c['x']
>>> a
{'w': 40, 'z': 10}
>>> del c['y']
Traceback (most recent call last):
...
KeyError: "Key not found in the first mapping: 'y'"
>>>

When a range of values ​​(e.g. variable programming language (i.e., global, local, etc.)) when, ChainMap particularly useful. In fact, there are ways to simplify:

>>> values = ChainMap()
>>> values['x'] = 1
>>> # Add a new mapping
>>> values = values.new_child() 
>>> values['x'] = 2
>>> # Add a new mapping
>>> values = values.new_child()
>>> values['x'] = 3
>>> values
ChainMap({'x': 3}, {'x': 2}, {'x': 1}) 
>>> values['x']
3
>>> # Discard last mapping
>>> values = values.parents
>>> values['x']
2
>>> # Discard last mapping
>>> values = values.parents
>>> values['x']
1
>>> values
ChainMap({'x': 1})
>>>

ChainMap As an alternative, consider the use of update () method of the dictionary combined. E.g:

>>> a = {'x': 1, 'z': 3}
>>> b = {'y': 2, 'z': 4}
>>> merged = dict(b)
>>> merged.update(a)
>>> merged['x']
1
>>> merged['y']
2
>>> merged['z']
3
>>>

This can usually be achieved, but the program need to create a separate dictionary object, or disruptive change existing dictionary object. Further, if the original dictionary object which is modified, and such modifications will not be reflected to the merged objects, i.e., objects are merged new object, and the original is not associated with a or b.

>>> a['x'] = 13
>>> merged['x']
1

ChainMap original dictionary, so it does not have this phenomenon.

>>> a = {'x': 1, 'z': 3 } 
>>> b = {'y': 2, 'z': 4 } 
>>> merged = ChainMap(a, b) 
>>> merged['x']
1
>>> a['x'] = 42 
>>> merged['x']    # Notice change to merged dicts
42
>>>

Guess you like

Origin www.cnblogs.com/jeffrey-yang/p/11318197.html