未加星标

ChainMap in Python

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

python also contains a container called “ ChainMap ” which encapsulates manydictionaries into one unit. ChainMap is member of module “ collections “.

Operations on ChainMap

Access Operations

1. keys() :-This function is used to display all the keys of all the dictionaries in ChainMap.

2. values() :-This function is used to display values of all the dictionaries in ChainMap.

3. maps :-This function is used to display keys with corresponding values of all the dictionaries in ChainMap.

# Please select Python 3 for running this code in IDE
# Python code to demonstrate ChainMap and
# keys(), values() and maps
# importing collections for ChainMap operations
import collections
# initializing dictionaries
dic1 = { 'a' : 1, 'b' : 2 }
dic2 = { 'b' : 3, 'c' : 4 }
# initializing ChainMap
chain = collections.ChainMap(dic1, dic2)
# printing chainMap using maps
print ("All the ChainMap contents are : ")
print (chain.maps)
# printing keys using keys()
print ("All keys of ChainMap are : ")
print (list(chain.keys()))
# printing keys using keys()
print ("All values of ChainMap are : ")
print (list(chain.values()))

Output :

All the ChainMap contents are :
[{'b': 2, 'a': 1}, {'c': 4, 'b': 3}]
All keys of ChainMap are :
['a', 'c', 'b']
All values of ChainMap are :
[1, 4, 2]

Note :Notice the key named “b” exists in both dictionaries, but only first dictionary key is taken as key value of “b”. Ordering is done as the dictionaries are passed in function.

Manipulating Operations

1. new_child() :-This function adds a new dictionary in the beginning of the ChainMap.

2. reversed() :-This function reverses the relative ordering of dictionaries in the ChainMap.

# Please select Python 3 for running this code in IDE
# Python code to demonstrate ChainMap and
# reversed() and new_child()
# importing collections for ChainMap operations
import collections
# initializing dictionaries
dic1 = { 'a' : 1, 'b' : 2 }
dic2 = { 'b' : 3, 'c' : 4 }
dic3 = { 'f' : 5 }
# initializing ChainMap
chain = collections.ChainMap(dic1, dic2)
# printing chainMap using map
print ("All the ChainMap contents are : ")
print (chain.maps)
# using new_child() to add new dictionary
chain1 = chain.new_child(dic3)
# printing chainMap using map
print ("Displaying new ChainMap : ")
print (chain1.maps)
# displaying value associated with b before reversing
print ("Value associated with b before reversing is : ",end="")
print (chain1['b'])
# reversing the ChainMap
chain1.maps = reversed(chain1.maps)
# displaying value associated with b after reversing
print ("Value associated with b after reversing is : ",end="")
print (chain1['b'])

Output :

All the ChainMap contents are :
[{'b': 2, 'a': 1}, {'b': 3, 'c': 4}]
Displaying new ChainMap :
[{'f': 5}, {'b': 2, 'a': 1}, {'b': 3, 'c': 4}]
Value associated with b before reversing is : 2
Value associated with b after reversing is : 3

This article is contributed by Manjeet Singh . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: Python
分页:12
转载请注明
本文标题:ChainMap in Python
本站链接:http://www.codesec.net/view/480981.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(29)