New changes in Python 3.9 scheduled to be released in October 2020 are summarized in the article What's new in Python 3.9 (Summary) started. I decided to cut out a relatively large amount of items in another article, but as the first step, I would like to take up the change that makes it possible to use the union operator in the dictionary type.
The dictionary type is one of the standard data types built into Python. In other languages, it is called various types such as hash type, map type, and associative array type, but the point is that it is a data type for storing name / value pairs (key-value pairs).
Consider that there are two dictionary-type data (d1, d2) and they are mixed into one dictionary-type data. There have been several ways to do this.
If you do d1.update (d2) `, the integrated dictionary type data will be overwritten and stored in d1. If you want to keep d1 as it is
e = d1.copy()
e.update(d2)
You need to make a copy and then call the ʻupdate` method. It's a little troublesome.
As {** d1, ** d2}
, the contents data is fetched from each dictionary type and returned in a new dictionary type. You can get integrated dictionary data without intermediate variables, but it's hard to tell what you're doing by looking at Pat. Even Guido, the original author of Python, is so confusing that he says, "I forgot that there was such a way" (laughs).
collections.ChainMap
The collections
module has a class called ChainMap
, and if you use dict (ChainMap (d2, d1))
, you can get integrated dictionary data. The tricky part is that, unlike the example above, if you have the same key, the one on the left has priority. Although standard, you have to import the collections
module, and this method isn't very intuitive either.
There is dict (mapping, ** kwarg)
in the dictionary type constructor, so if you use this to make dict (d1, ** d2)
, you will get integrated dictionary type data. By setting ** d2
, the contents of d2
are given as keyword arguments and added to the contents of the first argument ( d1
does not change). The problem with this method is that the contents of d2
are given as keyword arguments, so it can only be integrated if the key is a string dictionary type.
For operators used in Set type|
When|=
There is, for example, you can use it like this.
>>> a = set((1,2,3))
>>> b = set((3,4,5))
>>> a | b
{1,2,3,4,5}
>>> a |= b
>>> a
{1,2,3,4,5}
It is a union operator because it calculates the "sum" of two sets. Let's use this union operator for the integration of dictionary data, which is a change scheduled to enter 3.9 this time.
The specification is very simple, and the sum of the two dictionary type data is taken like the Set type. The difference from Set is the correspondence when the keys overlap, and even if the Set overlaps, it is only necessary not to count twice, but in the case of the dictionary type, there are values as well as keys, so which one to use Becomes a problem. With this change, the value on the right side of the formula is prioritized. It's an image of overlapping in order from the left and overwriting the ones with the same key.
It's faster to see it as an example.
>>> c = {'a': 1, 'b': 2}
>>> d = {'b': 3, 'c': 7}
>>> c | d
{'a': 1, 'b': 3, 'c': 7}
>>> d | c
{'a': 1, 'b': 2, 'c': 7}
>>> c |= d
>>> c
{'a': 1, 'b': 3, 'c': 7}
Herec | d
With the result ofd | c
The fact that the result of is different (not commutative) is unpleasant as an operator of "sum", but you can only think of it as such.
Let's explain the union operator that will be introduced in the dictionary type in Python 3.9 based on the contents of PEP-584. I did. I think that I used to use dict.update
in many cases, but since it destroys the original dictionary type, I think there are various uses for drawing immutable operations in easy-to-understand notation.
Recommended Posts