In [1]:
import collections
In [5]:
a= {'a':'A','c':'C'}
b= {'b':'B','c':'D'}
m=collections.ChainMap(a,b)
print('Indivisual Values')
print('a={}'.format(m['a']))
print('b={}'.format(m['b']))
print('c={}'.format(m['c']))
print()
print('Keys={}'.format(list(m.keys())))
print('Value={}'.format(list(m.values())))
print()
print("Print Items")
for k,v in m.items():
print('{}={}'.format(k,v))
print('"d" in m:{}'.format(('d' in m)))
Note The child mapping are searched in the order they are passed to the constructor, so the value reported for the key 'c' comes from the "a" dictionary.
In [6]:
import collections
a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}
m = collections.ChainMap(a, b)
print(m.maps)
print('c = {}\n'.format(m['c']))
# reverse the list
m.maps = list(reversed(m.maps))
print(m.maps)
print('c = {}'.format(m['c']))
In [7]:
import collections
a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}
m = collections.ChainMap(a, b)
print('Before: {}'.format(m['c']))
a['c'] = 'E'
print('After : {}'.format(m['c']))
ChainMap Provides a convenience method for creating a new instance with one extra mapping at the front of maps list to make it easy to avoid modifying the existing underlying data structures.
In [9]:
import collections
a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}
m1 = collections.ChainMap(a, b)
m2 = m1.new_child()
print('m1 before:', m1)
print('m2 before:', m2)
m2['c'] = 'E'
print('m1 after:', m1)
print('m2 after:', m2)
for situation where the new context is known or buit in advance, it is also possible to pass a mapping to `new_child()'
In [10]:
import collections
a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}
c = {'c': 'E'}
m1 = collections.ChainMap(a, b)
m2 = m1.new_child(c)
print('m1["c"] = {}'.format(m1['c']))
print('m2["c"] = {}'.format(m2['c']))
we can also skip the first map in the search.
parents
Property returning a new ChainMap
containing all of the maps in the current instance except the first one.
In [14]:
import collections
a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}
c = {'e': 'E', 'f': 'F'}
m1 = collections.ChainMap(a, b,c)
m1.parents
Out[14]:
In [ ]: