python?ChainMap的使用詳解
鏈接字典
chainMap是邏輯上合并兩個字典為一個邏輯單元,合并后的結(jié)構(gòu)實(shí)際上是一個列表,只是邏輯上是仍然為一個字典(并未生成新的),對此列表的操作模擬了各種字典的操作。合并后的取值及操作仍然是對原始字典的操作。
相同的key值合并后取第一個字典里的值作為重復(fù)key的值,
from collections import ChainMap
dict1={"x":1,"y":3,"t":12}
dict2={"x":5,"z":3}
chain_dict=ChainMap(dict1,dict2)
#相同的key值合并后取第一個dict里的值作為重復(fù)key的值
print(chain_dict["x"])
print(chain_dict["z"])結(jié)果:
1
3
對chain_dict的增刪改查影響的都是第一個字典
from collections import ChainMap
dict1={"x":1,"y":3,"t":12}
dict2={"x":5,"z":3}
chain_dict=ChainMap(dict1,dict2)
#對chain_dict的增刪改查影響的都是dict1
chain_dict["a"]=10
print(dict1)
chain_dict["x"]=100
print(dict1)
del dict1["t"]
print(dict1)
print(dict2)結(jié)果:
{'x': 1, 'y': 3, 't': 12, 'a': 10}
{'x': 100, 'y': 3, 't': 12, 'a': 10}
{'x': 100, 'y': 3, 'a': 10}
{'x': 5, 'z': 3}
maps屬性可輸出所以合并的字典
from collections import ChainMap
dict1={"x":1,"y":3,"t":12}
dict2={"x":5,"z":3}
chain_dict=ChainMap(dict1,dict2)
print(chain_dict.maps)結(jié)果:
[{'x': 1, 'y': 3, 't': 12}, {'x': 5, 'z': 3}]
new_child()方法是在合并后的映射列表頭部位置插入空映射{}
from collections import ChainMap
dict1={"x":1,"y":3,"t":12}
dict2={"x":5,"z":3}
chain_dict=ChainMap(dict1,dict2)
print(chain_dict.maps)
a=chain_dict.new_child()
print(a)
print(a.maps)結(jié)果:
[{'x': 1, 'y': 3, 't': 12}, {'x': 5, 'z': 3}]
ChainMap({}, {'x': 1, 'y': 3, 't': 12}, {'x': 5, 'z': 3})
[{}, {'x': 1, 'y': 3, 't': 12}, {'x': 5, 'z': 3}]
new_child()可以衍生出parent()方法的使用,parent()其實(shí)是在合并后的映射列表去掉頭部位置第一個映射后的結(jié)果:
from collections import ChainMap
dict1={"x":1,"y":3,"t":12}
dict2={"x":5,"z":3}
chain_dict=ChainMap(dict1,dict2)
print(chain_dict.maps)
a=chain_dict.new_child()
print(a)
print(a.maps)
b=a.parents
print("b=",b)
bb=b.parents
print("bb=",bb)結(jié)果:
[{'x': 1, 'y': 3, 't': 12}, {'x': 5, 'z': 3}]
ChainMap({}, {'x': 1, 'y': 3, 't': 12}, {'x': 5, 'z': 3})
[{}, {'x': 1, 'y': 3, 't': 12}, {'x': 5, 'z': 3}]
b= ChainMap({'x': 1, 'y': 3, 't': 12}, {'x': 5, 'z': 3})
bb= ChainMap({'x': 5, 'z': 3})
鏈接字典的應(yīng)用:
鏈接字典及它的new_child和parent方法特性適合處理作用域及查找鏈類似問題:
1,查找鏈
import builtins pylookup = ChainMap(locals(), globals(), vars(builtins))
2,作用域
比如用戶指定的命令行參數(shù)優(yōu)先于環(huán)境變量的示例,而環(huán)境變量優(yōu)先于默認(rèn)值:
import os, argparse
defaults = {'color': 'red', 'user': 'guest'}
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user')
parser.add_argument('-c', '--color')
namespace = parser.parse_args()
command_line_args = {k:v for k, v in vars(namespace).items() if v}
combined = ChainMap(command_line_args, os.environ, defaults)
print(combined['color'])
print(combined['user'])用例參考資料:
到此這篇關(guān)于python ChainMap的使用的文章就介紹到這了,更多相關(guān)python ChainMap的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python發(fā)送form-data請求及拼接form-data內(nèi)容的方法
這篇文章主要介紹了Python發(fā)送form-data請求及拼接form-data內(nèi)容的方法,文中采用的是requests的方式發(fā)送multipart/form-data請求,需要的朋友可以參考下2016-03-03
python PrettyTable模塊的安裝與簡單應(yīng)用
prettyTable 是一款很簡潔但是功能強(qiáng)大的第三方模塊,主要是將輸入的數(shù)據(jù)轉(zhuǎn)化為格式化的形式來輸出,這篇文章主要介紹了python PrettyTable模塊的安裝與簡單應(yīng)用,感興趣的小伙伴們可以參考一下2019-01-01
Python查找字符串中包含的多個元素的實(shí)現(xiàn)
本文詳細(xì)介紹了如何使用Python查找字符串中包含的多個元素,包括基本字符串操作和使用正則表達(dá)式進(jìn)行高級搜索,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Python數(shù)據(jù)結(jié)構(gòu)與算法之圖結(jié)構(gòu)(Graph)實(shí)例分析
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之圖結(jié)構(gòu)(Graph),結(jié)合實(shí)例形式分析了圖結(jié)構(gòu)的概念、原理、使用方法及相關(guān)操作技巧,需要的朋友可以參考下2017-09-09

