python ChainMap的使用和说明详解

yipeiwu_com6年前Python基础

ChainMap是Python collections模块中的一个成员,它用来将多个map组合到一起。Chain是链条的意思,字面意思就是把map串联到一起。

问题的背景是我们有多个字典或者映射,想把它们合并成为一个单独的映射,有人说可以用update进行合并,这样做的问题就是新建了一个数据结构以致于当我们对原来的字典进行更改的时候不会同步。如果想建立一个同步的查询方法,可以使用ChainMap

先看一下初步使用

from collections import ChainMap
a = {"x":1, "z":3}
b = {"y":2, "z":4}
c = ChainMap(a,b)
print(c)
print("x: {}, y: {}, z: {}".format(c["x"], c["y"], c["z"]))

输出:

ChainMap({'x': 1, 'z': 3}, {'y': 2, 'z': 4})
x: 1, y: 2, z: 3
[Finished in 0.1s]

这是ChainMap最基本的使用,可以用来合并两个或者更多个字典,当查询的时候,从前往后依次查询。

有一个注意点就是当对ChainMap进行修改的时候总是只会对第一个字典进行修改

In [6]: a = {"x":1, "z":3}

In [7]: b = {"y":2, "z":4}

In [8]: c = ChainMap(a, b)

In [9]: c
Out[9]: ChainMap({'z': 3, 'x': 1}, {'z': 4, 'y': 2})

In [10]: c["z"]
Out[10]: 3

In [11]: c["z"] = 4

In [12]: c
Out[12]: ChainMap({'z': 4, 'x': 1}, {'z': 4, 'y': 2})

In [13]: c.pop('z')
Out[13]: 4

In [14]: c
Out[14]: ChainMap({'x': 1}, {'z': 4, 'y': 2})

In [15]: del c["y"]
---------------------------------------------------------------------------
KeyError                 Traceback (most recent call last)
。。。。。。
KeyError: "Key not found in the first mapping: 'y'"

ChainMap和带有作用域的值,诸如全局变量,局部变量之间工作的时候特别有效,

In [4]: a = ChainMap()

In [5]: a["x"]=1

In [6]: a
Out[6]: ChainMap({'x': 1})

In [7]: b = a.new_child()

In [8]: b
Out[8]: ChainMap({}, {'x': 1})

In [9]: b["x"] = 2

In [10]: b
Out[10]: ChainMap({'x': 2}, {'x': 1})

In [11]: b["y"] = 3

In [12]: b
Out[12]: ChainMap({'x': 2, 'y': 3}, {'x': 1})

In [13]: a
Out[13]: ChainMap({'x': 1})

In [14]: c = a.new_child()

In [15]: c
Out[15]: ChainMap({}, {'x': 1})

In [16]: c["x"]
Out[16]: 1

In [17]: c["y"] = 1

In [18]: c
Out[18]: ChainMap({'y': 1}, {'x': 1})

In [19]: d = c.parents()
---------------------------------------------------------------------------
TypeError                 Traceback (most recent call last)
<ipython-input-19-dc4debb7ca3b> in <module>()
----> 1 d = c.parents()

TypeError: 'ChainMap' object is not callable

In [20]: d = c.parents

In [21]: d
Out[21]: ChainMap({'x': 1})

In [22]: d is a
Out[22]: False

In [23]: d == a
Out[23]: True

从原理上面讲,ChainMap实际上是把放入的字典存储在一个队列中,当进行字典的增加删除等操作只会在第一个字典上进行,当进行查找的时候会依次查找,new_child()方法实质上是在列表的第一个元素前放入一个字典,默认是{},而parents是去掉了列表开头的元素

In [24]: a = {"x":1, "z":3}

In [25]: b = {"y":2, "z":4}

In [26]: c = ChainMap(a,b)

In [27]: c
Out[27]: ChainMap({'x': 1, 'z': 3}, {'y': 2, 'z': 4})

In [28]: c.maps
Out[28]: [{'x': 1, 'z': 3}, {'y': 2, 'z': 4}]

In [29]: c.parents
Out[29]: ChainMap({'y': 2, 'z': 4})

In [30]: c.parents.maps
Out[30]: [{'y': 2, 'z': 4}]

In [31]: c.parents.parents
Out[31]: ChainMap({})

In [32]: c.parents.parents.parents
Out[32]: ChainMap({})

也正是因为底层是列表实现的,所以实际上ChainMap查询的字典实际上还是原来的字典的引用

ChainMap文档和示例:https://docs.python.org/3/library/collections.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python备份文件以及mysql数据库的脚本代码

复制代码 代码如下: #!/usr/local/python import os import time import string source=['/var/www/html/xxx...

分享给Python新手们的几道简单练习题

前言 本文主要给大家分享了一些简单的Python练习题,对学习python的新手们来说是个不错的练习问题,下面话不多说了,来一起看看详细的介绍吧。 第一题:使用while循环输入 1 2...

python中的格式化输出用法总结

本文实例总结了python中的格式化输出用法。分享给大家供大家参考,具体如下: Python一共有两种格式化输出语法。 一种是类似于C语言printf的方式,称为 Formatting...

Python的collections模块中namedtuple结构使用示例

namedtuple 就是命名的 tuple,比较像 C 语言中 struct。一般情况下的 tuple 是 (item1, item2, item3,...),所有的 item 都只能...

Python读取键盘输入的2种方法

Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘。如下: 1.raw_input 2.input raw_input函数 raw_input() 函数从标准输入...