python ChainMap的使用和说明详解

yipeiwu_com5年前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进阶_关于命名空间与作用域(详解)

写在前面 如非特别说明,下文均基于Python3 命名空间与作用于跟名字的绑定相关性很大,可以结合另一篇介绍Python名字、对象及其绑定的文章。 1. 命名空间 1.1 什么是命名空间...

Python3 中作为一等对象的函数解析

Python3 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。 函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如pr...

Python使用正则表达式实现文本替换的方法

本文实例讲述了Python使用正则表达式实现文本替换的方法。分享给大家供大家参考,具体如下: 2D客户端编程从某种意义上来讲就是素材组织,所以,图片素材组织经常需要批量处理,python...

利用pyinstaller打包exe文件的基本教程

前言 PyInstaller可以用来打包python应用程序,打包完的程序就可以在没有安装Python解释器的机器上运行了。PyInstaller支持Python 2.7和Python...

Python中处理字符串之islower()方法的使用简介

 islower()方法判断检查字符串的所有的字符(字母)是否为小写。 语法 以下是islower()方法的语法: str.islower() 参数  ...