当前位置: 移动技术网 > IT编程>脚本编程>Python > python ChainMap的使用和说明详解

python ChainMap的使用和说明详解

2019年07月20日  | 移动技术网IT编程  | 我要评论

蒙古国网站,6070电影下载,路遥功放

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#collections.chainmap

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网