Get the App
SLTechnology News&Howtos  ›  Development  › 

How to instantiate ChainMap in python

Shulou Source: shulou.com Published: 2022-06-03 15:34:06 09月22日 Update

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!

Tags: Examples parameters multiple dictionaries articles different code single finished object situation more knowledge type industry view information information channel channel medium creation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL MariaDB Xiaomi NVidia