Try using the collections module (ChainMap) of python3

collections collections contain container data types. In python3 (3.4.3), there are some additions to the one in python2.7. Reference URL: http://docs.python.jp/3.4/library/collections.html The points added from python2.7 are as follows.

ChainMap: A dictionary-like class that creates a single view of multiple mappings UserDict: A dictionary object wrapper that simplifies dictionary subclassing UserList: List object wrapper that makes it easy to subclass lists UserString: A wrapper for string objects that makes it easy to subclass strings

User ~ is usually a class wrapper, so the explanation is omitted.

ChainMap

Basic

The code I wrote this time is on GitHub. (https://github.com/KodairaTomonori/Qiita/blob/master/default_module/collections/test_ChainMap.py) First, from the simple usage of ChainMap

test_ChainMap.py


import collections
import see
dict_a = dict(a=1, b=2, c=3)
dict_b = dict(x=10, y=11, z=12)
def get_name(arg_id, arg_values):
    for key, value in arg_values.items():
        if id(value) == arg_id: return key
    return 'NotDefined'

def print_arguments(*args, arg_values):
    for arg in args:
        print(get_name(id(arg), arg_values) + ' : ' + repr(arg) )

chain_map = collections.ChainMap(dict_a, dict_b)
child = chain_map.new_child()
print('make_chain_maps')
print_arguments(chain_map, child, arg_values=locals() )
print('chain_map.maps: ' + repr(chain_map.maps) )

output

make_chain_maps chain_map : ChainMap({'b': 2, 'c': 3, 'a': 1}, {'z': 12, 'x': 10, 'y': 11}) child : ChainMap({}, {'b': 2, 'c': 3, 'a': 1}, {'z': 12, 'x': 10, 'y': 11}) chain_map.maps: [{'a': 1, 'c': 3, 'b': 2}, {'y': 11, 'z': 12, 'x': 10}]

Description

As the name suggests, ChainMap connectsmap (dictionaly). You can keep each one as a dictionary, not just connect them. So, as you can see from the output, there are separate dict_a and dict_b. If you do chain ['z'] and chain_map ['b'], you will get '2 and 12', respectively. When you use it, you can use it normally as one dict. By setting .new_child (), a new dict will be created at the beginning. By setting .maps, ChainMap will be listed and returned.

Although not written in the code, you can use .items (), .keys (), .values () like normal dict. Looking at the reference URL, it seems that the search is from the front and the others are only from the front.

The search continues to search for the underlying mapping until the key is found. In contrast, write, update, and delete operate only on the first mapping.

Child and parent updates

Next, we will update.

update.py


child.update(d=4, a=0)
print('update_child_map')
print_arguments(chain_map, child, arg_values=locals() )
print()

chain_map.update(z=100)
print('update_parent_map')
print_arguments(chain_map, child, arg_values=locals() )

Output 2

update_child_map chain_map : ChainMap({'a': 1, 'c': 3, 'b': 2}, {'y': 11, 'z': 12, 'x': 10}) child : ChainMap({'a': 0, 'd': 4}, {'a': 1, 'c': 3, 'b': 2}, {'y': 11, 'z': 12, 'x': 10}) update_parent_map chain_map : ChainMap({'a': 1, 'z': 100, 'c': 3, 'b': 2}, {'y': 11, 'z': 12, 'x': 10}) child : ChainMap({'a': 0, 'd': 4}, {'a': 1, 'z': 100, 'c': 3, 'b': 2}, {'y': 11, 'z': 12, 'x': 10})

Explanation 2

Writing or updating to the child (leftmost dict) does not change the parent (dict other than the leftmost). Also, if you change the parent, the change will be reflected in'child'. The pointers of the common elements of chain_map and child seem to point to the same place.

merge

How to make one dictionary when something is messy and hard to see. You can simply enclose it in dict ().

merge.py


print('each_Chain')
print('chain_map:', dict(**chain_map) )
print('child    :', dict(**child) )

output

each_Chain chain_map: {'x': 10, 'z': 100, 'c': 3, 'b': 2, 'a': 1, 'y': 11} child : {'x': 10, 'z': 100, 'c': 3, 'b': 2, 'a': 0, 'd': 4, 'y': 11}

Summary

ChainMap is convenient, but I can't think of any use for it.

This class can be used to simulate nested scopes and is useful for templating.

Although it is written in the reference URL, I do not understand well because it is only katakana. It seems that it can be used to check the update of weights when learning with NLP.

Recommended Posts

Try using the collections module (ChainMap) of python3
Try using the Python Cmd module
View using the python module of Nifty Cloud mobile backend
Python collections module
Try using the Wunderlist API in Python
Pass the path of the imported python module
[Python] Try pydash of the Python version of lodash
Check the path of the Python imported module
Try the python version of emacs-org parser orgparse
[Python] Let's execute the module regularly using schedule
Try using the BitFlyer Ligntning API in Python
Python: Try using the UI on Pythonista 3 on iPad
the zen of Python
Try using the Python web framework Tornado Part 1
Try the free version of Progate [Python I]
I tried using the Datetime module by Python
[Python] A rough understanding of the logging module
Find the geometric mean of n! Using Python
Try using the Python web framework Tornado Part 2
Try using Tweepy [Python2.7]
Try using the DropBox Core API in Python
Explanation of the concept of regression analysis using python Part 2
Cut a part of the string using a Python slice
Try projective transformation of images using OpenCV with Python
The pain of gRPC using Python. November 2019. (Personal memo)
Let's use the Python version of the Confluence API module.
Explanation of the concept of regression analysis using Python Part 1
Write data to KINTONE using the Python requests module
Explanation of the concept of regression analysis using Python Extra 1
Study from the beginning of Python Hour8: Using packages
Towards the retirement of Python2
About the Python module venv
Try using Kubernetes Client -Python-
Automatic update of Python module
Try using the Twitter API
About the ease of Python
python: Basics of using scikit-learn ①
Try using the Twitter API
Try using the PeeringDB 2.0 API
About the features of Python
The Power of Pandas: Python
Try scraping the data of COVID-19 in Tokyo with Python
Try to get the function list of Python> os package
Try using the Python web framework Django (2) --Look at setting.py
Make the display of Python module exceptions easier to understand
Try a similar search for Image Search using the Python SDK [Search]
Solve the Japanese problem when using the CSV module in Python.
What is the default TLS version of the python requests module?
[Python] I tried collecting data using the API of wikipedia
2015-11-26 python> Display the function list of the module> import math> dir (math)
Test the version of the argparse module
The story of Python and the story of NaN
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Try the Python LINE Pay SDK
A brief summary of Python collections
Existence from the viewpoint of Python
Try using Pleasant's API (python / FastAPI)
pyenv-change the python version of virtualenv
Try using LevelDB in Python (plyvel)
Change the Python version of Homebrew