Python 3 list derivation of a little understanding!
The list derivation of Python is generally difficult for beginners to understand, simply look at an example:
[x * x for x in range (1,100)]
The above is a very simple list deduction, I understand the way from right to left to see.
At 1.1, let's start with a range (1,100) to generate an iterator object (a list of 1-99), then iterate over each object of the list, then have each object perform a square operation and finally generate a list.
-----------------------------Split Line----------------------------------------------------------------------------------------- ------------------------------------------------------------
This may seem a bit more intuitive.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We can also use this:
[' The%s '% x for x in range (1,10)]
Here we use the formatted string%s, which is the same as it is understood. Right-to-left understanding: First generate a list with range (), then use for in to iterate over each object, and then put each object into the placeholder, and finally generate a list.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[(x, Y) for x in range (1,100) for Y in range (1,100)]
Using the derivation list to generate a tuple, understand: First generate a list with range (), and then iterate, this operation performed two times, X at a time, y at a time, and then a list is generated using the tuple definition.
Python 3 list derivation of a little understanding!