How to instantiate ChainMap in python
Xiaobian to share with you how to instantiate ChainMap in python, I hope you have something to gain after reading this article, let's discuss it together!
description
To create ChainMap in Python code, you need to import collections from the class and call.
Class initializers can take zero or more mappings as parameters. No arguments, it initializes a chain map with an empty dictionary.
ChainMap can create multiple objects using different combinations of mappings. In each case, ChainMap returns a single dictionary-like view of the input map. Any type of mapping can be used, such as OrderedDict and defaultDict.
examples
>>> from collections import ChainMap>>> from collections import OrderedDict, defaultdict >>> # Use no arguments>>> ChainMap()ChainMap({}) >>> # Use regular dictionaries>>> numbers = {"one": 1, "two": 2}>>> letters = {"a": "A", "b": "B"} >>> ChainMap(numbers, letters)ChainMap({'one': 1, 'two': 2}, {'a': 'A', 'b': 'B'}) >>> ChainMap(numbers, {"a": "A", "b": "B"})ChainMap({'one': 1, 'two': 2}, {'a': 'A', 'b': 'B'}) >>> # Use other mappings>>> numbers = OrderedDict(one=1, two=2)>>> letters = defaultdict(str, {"a": "A", "b": "B"})>>> ChainMap(numbers, letters)ChainMap( OrderedDict([('one', 1), ('two', 2)]), defaultdict(, {'a':' A','b':' B'})) After reading this article, I believe you have a certain understanding of "how to instantiate ChainMap in python". If you want to know more about it, please pay attention to the industry information channel. Thank you for reading!