Advanced features of getting started with Python
When learning advanced features, you may feel a little difficult. these new features have never been met before in c/c ++, c/c ++ does not support such simple but powerful syntax.
Secondary slice
When it comes to slicing, we can think of splitting a piece of radish. we can use this to compare the slicing here. In python, the slice operation is to take a segment in the list or tuple.
For example, a list is defined as follows:
# Define a list
L = ['luffy', 'Cory', 'Nancy ', 'Jeffrey', 'Kyle ', 'Avery', 'Jason ', 'sunny']
There are two methods to retrieve the first three elements. the code is as follows:
>>> L [: 3]
['Luffy', 'Cory', 'Nancy ']
>>> L [0: 3]
['Luffy', 'Cory', 'Nancy ']
That is to say, it can be omitted from 0.
The code for getting 2nd to 4th elements is as follows:
>>> L [1: 5]
['Cory', 'Nancy ', 'Jeffrey', 'kil']
Take the last 2nd records until the last code is as follows:
>>> L [-2:]
['Jason ', 'sunny']
The code is as follows:
>>> L [:]
['Luffy', 'Corey ', 'Nancy', 'Jeffrey ', 'Kyle', 'Avery ', 'Jason', 'sunny ']
Take one of every two in the entire list:
>>> L [: 2]
['Luffy', 'Nancy ', 'Kyle', 'Jason ']
In the first 6 of the list, take 1 for each 2:
>>> L [: 5: 2]
['Luffy', 'Nancy ', 'kil']
In the actual editing code, you can often replace the variable pointing to the list in the slicing operation with the list itself, as shown below:
>>> ['Luffy', 'Corey ', 'Nancy', 'Jeffrey ', 'Kyle', 'Avery ', 'Jason', 'sunny '] [: 3]
['Luffy', 'Cory', 'Nancy ']
Tuple and string can both be considered as a list, so the above syntax can also be used for it.
Triplicate
List ComPRehensions is a powerful built-in list generator in python.
The simplest example is to create a list from 1 to 10.
List (range (1, 11 ))
This is a simple line of code. But what should I do to generate a more complex list?
What should I do to generate a list like [1x1, 2, 2, 3x3,..., 10x10? Loop can be implemented, but the code is too complex. to use the list generation method, you only need the following line of code:
>>> [X * x for x in range ()]
[1, 4, 9, 16, 25, 36, 49,64, 81,100]
If you add only the square of the even number, it is not complicated. you only need to add the if judgment condition after the for loop.
>>> [X * x for x in range (1, 11) if x % 2 = 0]
[4, 16, 36, 64,100]
You can also use two-layer loops,
>>> [X + y for x in 'abc' for y in 'XYZ']
['Ax ', 'ay', 'Az', 'bx ', 'By', 'BZ', 'cx', 'cy ', 'cz']
After writing so many list generators, I think everyone understands the origins of this syntax. In fact, the normal syntax is reversed, and the bottom layer of computing is placed at the top. For example, the last list generator can be written as the following normal syntax:
For x in 'ABC ':
For y in 'XYZ ':
Print (x + y)
Four generators
With the list generation formula, we can directly create a list. However, if we cannot create a list with a large number of elements considering the memory limit, what should we do?
Python provides a mechanism called generator, which can be used to calculate subsequent elements, so that you do not need to create all elements at a time. There are two methods to create a generator:
Method 1: Change [] of the list generation type to (), and a generator is created.
As shown in the following code:
>>> G = (x for x in range (10 ))
>>> Print (next (g ))
0
To obtain the first element of the generator, you can call the next function directly on the G. Of course, you can also use the for loop to traverse the data generated by the generator in the next step.
Method 2: if the calculation method is too complex and the list generation method cannot be implemented, it can be implemented through functions. Compared with the list Generator to Generator, the function to Generator is also very simple. you only need to write the function first, and then add the yield keyword to a certain position.
For example, the function for generating a Fibonacci series is as follows:
Def fib (max ):
N, a, B = 0, 0, 1
While n <max:
Print (B)
A, B = B, a + B
N = n + 1
Return 'done'
To convert this function to generator, you only need to replace the print (B) line of code with yield B.
Def fib (max ):
N, a, B = 0, 0, 1
While n <max:
Yield B
A, B = B, a + B
N = n + 1
Return 'done'
A function containing the yield keyword is no longer a function, but a generator. It should be noted that the execution process of the generator is different from that of the function. it is returned when yield is encountered, and the execution will continue from the yield returned last time when the next call is made.
Five iterators
Objects that can directly act on the for loop are collectively called iteration objects: Iterable. you can use isinstance () to determine whether an object is an Iterable object. The generator can not only traverse through the for loop, but also use next () traversal. The object that can be called by the next () function and continuously return the next value is collectively referred to as the Iterator: Iterator.
Here we need to distinguish between Iterable and Iterator, list, dict, and str. they are both Iterable, but they are not Iterator (you can use iter () function to change Iterable to Iterator ). Iterator can be an infinitely large data stream and cannot know the length of the entire sequence in advance. these are the requirements that Iterable cannot meet.
Postscript
I have learned some new programming features in python. If any error occurs, please leave a message !!!
The above is the content of advanced features for getting started with Python. For more articles, please follow the PHP Chinese network (www.php1.cn )!