The text and pictures of this article come from the Internet and are only for learning and communication. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling
The following article comes from Tencent cloud, written by brother Liang in the workplace
(want to learn Python? Python learning exchange group: 1039649593, meet your needs. All materials have been uploaded to the group file stream and can be downloaded by yourself! There are also a large number of the latest 2020 Python learning materials.)
Python deconstruction and encapsulation
Ask questions
Look at the following code first
x = 1 y = 2 tmp = x x = y y = tmp print(x, y)
The output of the code is: 2 1
Look at the following code:
x = 1 y = 2 x, y = y, x print(x, y)
The output of the code is: 2 1
x. The meaning behind the code y = y, X is deconstruction and encapsulation
Python encapsulation
In [1]: t = 1, 2 In [2]: t Out[2]: (1, 2) In [3]: type(t) Out[3]: tuple # Parentheses can be omitted when defining tuples In [4]: t1 = (1, 2) In [5]: t2 = 1, 2 # t1 and t2 are equivalent In [6]: t1 Out[6]: (1, 2) In [7]: t2 Out[7]: (1, 2)
Therefore, the encapsulated result must be tuples.
x. Y = y, x the right side of the code will be encapsulated as (y, x)
Python deconstruction
Basic deconstruction
In [8]: lst = [1, 2] In [9]: first, second = lst In [10]: print(first, second) 1 2
Assign the elements of the linear structure lst to the variables first and second according to the element order
Asterisk deconstruction
In [11]: lst = list(range(5)) In [12]: head, *tail = lst In [13]: head Out[13]: 0 In [14]: tail Out[14]: [1, 2, 3, 4] In [15]: *lst2 = lst # There must be a variable with an asterisk on the left File "<ipython-input-15-98211a44ccfb>", line 1 *lst2 = lst ^ SyntaxError: starred assignment target must be in a list or tuple In [16]: *head, tail = lst In [17]: head Out[17]: [0, 1, 2, 3] In [18]: lst Out[18]: [0, 1, 2, 3, 4] In [19]: tail Out[19]: 4 In [20]: head, *m1, *m2, tail = lst # There cannot be more than one asterisk. There can only be one asterisk File "<ipython-input-20-1fc1a52caa8e>", line 1 head, *m1, *m2, tail = lst ^ SyntaxError: two starred expressions in assignment In [21]: v1, v2, v3, v4, v5, v6, v7 = lst # The number of variables on the left cannot exceed the number of elements on the right --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-21-9366cfb498a1> in <module>() ----> 1 v1, v2, v3, v4, v5, v6, v7 = lst ValueError: not enough values to unpack (expected 7, got 5) In [22]: v1, v2 = lst #The number of variables on the left cannot be less than the number of elements on the right --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-22-d7b0a4e7871e> in <module>() ----> 1 v1, v2 = lst ValueError: too many values to unpack (expected 2)
It can be summarized as the following rules:
- Elements are assigned to variables in order
- Variables and elements must match
- Asterisk variables can accept any number of elements
- Variables with asterisks cannot appear alone
Multilevel deconstruction
Deconstruction supports multiple levels
In [23]: lst = [1, (2, 3), 5] In [24]: _, v, *_ = lst # v resolves to (2, 3) In [25]: v Out[25]: (2, 3) In [26]: _, val = v # v can be further deconstructed In [27]: val Out[27]: 3 In [28]: _, (_, val), *_ = lst # It can be deconstructed in one step In [29]: val Out[29]: 3 In [30]: _, [*_, val], *_ = lst # Middle part solution composition list In [31]: val Out[31]: 3 In [32]: _, _, val, *_ = lst # (2, 3) parse into the second_ In [33]: val Out[33]: 5
Use of Python underscores
Use single underline_ Indicates that the variable is discarded, which is a python convention. A single underscore is also a legal identifier for Python, but if you don't want to discard a variable, you usually don't use a single underscore to represent a meaningful variable. It can be understood as a convention.
Use of deconstruction and encapsulation
For very complex data structures and multi-layer nested linear structures, deconstruction can be used to quickly extract the values, which is very convenient
For example, the following usage methods
In [1]: key, _, value = 'I love Python'.partition(' love ') In [2]: key Out[2]: 'I' In [3]: value Out[3]: 'Python'