首页 > 文章列表 > python中ChainMap如何创建对象

python中ChainMap如何创建对象

Python ChainMap
143 2022-08-07

说明

1、使用ChainMap类方法创建对象.fromkeys()。

2、使用可迭代键和所有键的可选默认值。若调用.fromkeys()上的ChainMap和迭代键作为参数,则得到的链条地图为字典。

键来自输入可迭代对象,默认值为None。或者,第二个参数可以传输。fromkeys()为每个键提供合理的默认值。

实例

>>> from collections import ChainMap
 
>>> ChainMap.fromkeys(["one", "two","three"])
ChainMap({'one': None, 'two': None, 'three': None})
 
>>> ChainMap.fromkeys(["one", "two","three"], 0)
ChainMap({'one': 0, 'two': 0, 'three': 0})