Steps to implement python ChainMap merge dictionary

  • 2021-06-28 13:00:42
  • OfStack

Dictionaries are the only mapping type in the Python language.

The hash value (key, key) in a map type object and the object pointed to (value, value) are one-to-many relationships and are generally considered variable hash tables.
A dictionary object is mutable and is a container type that can store any number of Python objects, including other container types.

The difference between dictionary type and sequence type:

1. Different ways of accessing and accessing data.
2. Sequence type uses only numeric keys (indexed numerically from the beginning of the sequence);
3. Mapping types can use other object types as keys (e.g., numbers, strings, meta-ancestors, 1 typically using strings as keys), unlike sequence types, the keys of mapping types are directly 4. connected or indirectly associated with stored data values.
5. The data in the mapping type is out of order.This is different from the sequence type, which is arranged in numerical order.
6. Mapping types use keys to "map" directly to values.

The dictionary is one of the most powerful data types in Python.

The ChainMap object supports all the methods of a dictionary object, and you can manipulate it exactly like you do with dictionary 1.But instead of really merging dictionaries together, it stores an Key mapping internally to each dictionary. When you read e [key], it first queries which dictionary the key is in, and then queries the corresponding dictionary for values.So using ChainMap requires little extra memory space (of course this object takes up a bit of space on its own, but if you want to merge a large dictionary, its own space is almost negligible).


from collections import ChainMap

a = {'a': 1, 'b': 2}
b = {'x': 3, 'y': 4}
a.update(b)
print(a)

c = ChainMap(a, b)
print(c['a'])

If one of the two dictionaries has the same Key name, the ChainMap object will use the value from the first dictionary that has this Key


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

c = ChainMap(a, b)
print(c['a'])

If you add an Key-Value pair to the ChainMap object, the new Key-Value will be added to the first dictionary


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

c = ChainMap(a, b)
c['new'] = " New Value "
print(a)

If you delete an Key from the original dictionary, the ChainMap object will be updated accordingly


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

c = ChainMap(a, b)
print('w' in c)
a['w'] = ' New Value '
print('w' in c)

If an Key is deleted from the ChainMap object and if the Key only exists in one source dictionary, the Key will be deleted from the source dictionary.If this Key exists in more than one dictionary, Key will be deleted from the first dictionary.When deleted from the first dictionary, the second source

The Key of the dictionary can be 继Continued read by ChainMap

Cannot delete key that does not exist in the first dictionary a


a = {'a': 1, 'b': 2}
b = {'aa': 3, 'a': 4}

c = ChainMap(a, b)
c.pop('a') #  Can only be deleted  a  In  k , delete  b  Errors will occur 
print(a, b)
print(c['a'])


Related articles: