filmov
tv
ChainMap In Python: 7 Things (2 Min) To Know | ChainMap Container Collections Module

Показать описание
In this tutorial, you'll learn how to use ChainMap container in Python from the collections modules.
—
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn seven ways to use the chain map container from the collections module in Python. Number one, the chain map groups multiple dictionaries together to create a single updatable view.
It returns a dictionary-like view of all input mappings. Here I'm creating a chain map from a regular dictionary an ordered dict and a default dick. Number two, you can access key-value pairs in a chain map just like a regular dictionary and if the key doesn't exist you'll get a key error.
Number three, when there are duplicate keys in a chain map the first occurrence gets returned. In this example, I have a customer key in both table dict and table od. In this chain map the customer key from the table dick gets returned.
Number four, you can use a for loop with the items method to iterate over the items in a chain map just like a regular dictionary. Number five, updating or deleting a key will only affect the first mapping in a chain map. In this example, I'm first adding a new key called sales to the first mapping then deleting the key cells, and finally clearing the whole first mapping.
Note, the second mapping of table dict 5b was not affected by all these operations. Number six, use the maps dot reverse method to reverse the mapping in a chain map. Last but not the least number seven.
Use the new underscore child method to add a child mapping in the front and return a new chain map object. Here I'm adding a new child called table to underscore dict 7b to an existing chain map and returning a new chain map object. Note, you can use the dot parents property to access just the parents as well.
There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.
—
from collections import ChainMap, OrderedDict, defaultdict
# 1. ChainMap groups multiple dicts together to create a single, update-able view
# returns a single dictionary like view of all the input mappings
table_dict1 = {'property': 5, 'customer': 6}
table_od1 = OrderedDict(sales=4, taxes=5)
table_dd1 = defaultdict(lambda: 0, {'finance': 7})
print('1.', ChainMap(table_dict1, table_od1, table_dd1))
# 2. Access keys/values just like a regular dictionary
# KeyError if the key doesn't exist
table_dict2 = {'property': 5, 'customer': 6}
table_od2 = OrderedDict(sales=4, taxes=5)
table_chainmap2 = ChainMap(table_dict2, table_od2)
print('2a.', table_chainmap2['sales'], table_chainmap2['customer'])
# print('2b.', table_chainmap2['xyz'])
# 3. first occurrence gets returned for duplicate keys
table_dict3 = {'property': 5, 'customer': 6}
table_od3 = OrderedDict(sales=4, customer=5)
table_chainmap3 = ChainMap(table_dict3, table_od3)
print('3.', table_chainmap3['customer'])
# 4. for loop with items() method to iterate over the chainmap
table_dict4 = {'property': 5, 'customer': 6}
table_od4 = OrderedDict(sales=4, customer=5)
table_chainmap4 = ChainMap(table_dict4, table_od4)
print('4.', key, value)
# 5. update/delete will only affect the first mapping
table_dict5a = {'property': 5, 'customer': 6}
table_dict5b = {'sales': 5, 'customer': 7}
table_chainmap5 = ChainMap(table_dict5a, table_dict5b)
print('5a.', table_chainmap5)
table_chainmap5['sales'] = 8
print('5b.', table_chainmap5)
del table_chainmap5['sales']
print('5c.', table_chainmap5)
print('5d.', table_chainmap5)
table_dict6 = {'property': 5, 'customer': 6}
table_od6 = OrderedDict(sales=4, customer=5)
table_chainmap6 = ChainMap(table_dict6, table_od6)
print('6a.', table_chainmap6)
print('6b.', table_chainmap6)
# 7. Use new_child() method to add a child mapping in the front and return a new chainmap object
table_dict7 = {'property': 5, 'customer': 6}
table_od7 = OrderedDict(sales=4, customer=5)
table_chainmap7a = ChainMap(table_dict7, table_od7)
print('7a.', table_chainmap7a)
table_dict7b = {'taxes': 8}
print('7b.', table_chainmap7b)
—
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn seven ways to use the chain map container from the collections module in Python. Number one, the chain map groups multiple dictionaries together to create a single updatable view.
It returns a dictionary-like view of all input mappings. Here I'm creating a chain map from a regular dictionary an ordered dict and a default dick. Number two, you can access key-value pairs in a chain map just like a regular dictionary and if the key doesn't exist you'll get a key error.
Number three, when there are duplicate keys in a chain map the first occurrence gets returned. In this example, I have a customer key in both table dict and table od. In this chain map the customer key from the table dick gets returned.
Number four, you can use a for loop with the items method to iterate over the items in a chain map just like a regular dictionary. Number five, updating or deleting a key will only affect the first mapping in a chain map. In this example, I'm first adding a new key called sales to the first mapping then deleting the key cells, and finally clearing the whole first mapping.
Note, the second mapping of table dict 5b was not affected by all these operations. Number six, use the maps dot reverse method to reverse the mapping in a chain map. Last but not the least number seven.
Use the new underscore child method to add a child mapping in the front and return a new chain map object. Here I'm adding a new child called table to underscore dict 7b to an existing chain map and returning a new chain map object. Note, you can use the dot parents property to access just the parents as well.
There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.
—
from collections import ChainMap, OrderedDict, defaultdict
# 1. ChainMap groups multiple dicts together to create a single, update-able view
# returns a single dictionary like view of all the input mappings
table_dict1 = {'property': 5, 'customer': 6}
table_od1 = OrderedDict(sales=4, taxes=5)
table_dd1 = defaultdict(lambda: 0, {'finance': 7})
print('1.', ChainMap(table_dict1, table_od1, table_dd1))
# 2. Access keys/values just like a regular dictionary
# KeyError if the key doesn't exist
table_dict2 = {'property': 5, 'customer': 6}
table_od2 = OrderedDict(sales=4, taxes=5)
table_chainmap2 = ChainMap(table_dict2, table_od2)
print('2a.', table_chainmap2['sales'], table_chainmap2['customer'])
# print('2b.', table_chainmap2['xyz'])
# 3. first occurrence gets returned for duplicate keys
table_dict3 = {'property': 5, 'customer': 6}
table_od3 = OrderedDict(sales=4, customer=5)
table_chainmap3 = ChainMap(table_dict3, table_od3)
print('3.', table_chainmap3['customer'])
# 4. for loop with items() method to iterate over the chainmap
table_dict4 = {'property': 5, 'customer': 6}
table_od4 = OrderedDict(sales=4, customer=5)
table_chainmap4 = ChainMap(table_dict4, table_od4)
print('4.', key, value)
# 5. update/delete will only affect the first mapping
table_dict5a = {'property': 5, 'customer': 6}
table_dict5b = {'sales': 5, 'customer': 7}
table_chainmap5 = ChainMap(table_dict5a, table_dict5b)
print('5a.', table_chainmap5)
table_chainmap5['sales'] = 8
print('5b.', table_chainmap5)
del table_chainmap5['sales']
print('5c.', table_chainmap5)
print('5d.', table_chainmap5)
table_dict6 = {'property': 5, 'customer': 6}
table_od6 = OrderedDict(sales=4, customer=5)
table_chainmap6 = ChainMap(table_dict6, table_od6)
print('6a.', table_chainmap6)
print('6b.', table_chainmap6)
# 7. Use new_child() method to add a child mapping in the front and return a new chainmap object
table_dict7 = {'property': 5, 'customer': 6}
table_od7 = OrderedDict(sales=4, customer=5)
table_chainmap7a = ChainMap(table_dict7, table_od7)
print('7a.', table_chainmap7a)
table_dict7b = {'taxes': 8}
print('7b.', table_chainmap7b)
Комментарии