Python list ParsingI feel like python is very flexible. When I first came into contact with it, especially when I learned other languages,
You may feel unaccustomed. How can you see what is wrong? What is strange? This is the magic of list resolution.
Python list Parsing is one of the python iterations, which is much faster than the python for loop. Next let's take a look at Python list parsing.
How it works.
Example:
A = [x + 2 for X in range (10)]
Print
Output result:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
The execution process of the above example is as follows:
Python iterates range (10) In the interpreter, extracts the content in the list in sequence, assigns the value to the leftmost X, and then executes the X + 2 operation,
And save the execution result in the list. After range (10) iteration, a new list is generated. The result is [2, 3, 4, 5, 6, 7, 8, 9, 10, 11].
It can be seen from the above that this is also a method for creating a python list.
If the above example is implemented using a python for loop, what would it be like?
A = []
For I in range (10 ):
A. append (I + 2)
Print
Output result:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
As can be seen from the above, the python list Parsing is better than the python for loop parsing listCodeLess data, faster resolution, cool writing, and independent writing
The script is very simple, but it also has some shortcomings, and its readability is not very good, especially in the case of team development, it should be less useful, so the versatility is better, it is also good for everyone.
If you are interested in the python list or Python list operation method, you can check it out!
Author: Lao Wang @ Python tutorial
Old Wang Python provides Python tutorials and Python downloads related to pythn.