map is also called ChainMap – a type of data structure to manage multiple dictionaries together as one unit.
It eliminates any duplicate keys by containing keys and value pairs in a specific sequence.
creating a ChainMap
we create two dictionaries and club them using the ChainMap method from the collection library, Then we print the keys and values of the result of the combination dictionaries if there are duplicate keys, then only the value from the first key is preserved.
#Example of creating a map in Python
import collections
dict1 = {'day1':'Mon','day2':'Tue','day3':'Wen'}
dict = {'day4':'Thur','day5':'Fri'}
res = collections.ChainMap(dict1,dict)
#creating a single dictionary
print(res.maps,'\n')
print('Keys = {}'.format(list(res.keys())))
print('Values = {}'.format(list(res.values())))
print()
#Find a specific value in the result
print('day1 in res:{}'.format(('day1' in res)))
print('day3 in res:{}'.format(('day2' in res)))
print('day4 in res:{}'.format(('day4' in res)))
print('day5 in res:{}'.format(('day5' in res)))
Enter fullscreen mode Exit fullscreen mode
when the above code is executed produces the following result.
day1 in res: True
day3 in res: True
day4 in res: True
day5 in res: True
Enter fullscreen mode Exit fullscreen mode
原文链接:Map in Python.
暂无评论内容