Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
As mentioned above, we are once again familiar with loop control in Python. Now we will contactIterable object).
The main purpose of this lecture is to read PythonProgramWhen there is a basic concept for the loop object.
The circular object does not exist with the birth of Python, But it develops rapidly, especially in the python 3x era. From the perspective of zip () or map () changes, the circular object is becoming the standard form of the loop.
1. What is a circular object?
A circular object is such an object that containsNext () method(_ Next _ () method, in Python 3x). The purpose of this method is to get the next result.Stopiteration Error.
When a loop structure (such as for) calls a loop object, it calls the next () method every time it loops until the stopiteration occurs and the for loop receives, you will know that the loop has ended and stop calling next ().
Suppose we have a test.txt file:
1234Abcdefg
Run the following Python command line:
>>> F = open('test.txt ')
>>> F. Next ()
>>> F. Next ()
...
Enter F. Next () until the stopiteration occurs.
Open () returns a circular object., Including the next () method. The next () method returns the content of a new row each time, and the stopiteration is cited at the end of the file. In this way, we perform a loop manually.
If it is automatically executed, it is:
ForLineInOpen ('Test.txt'):PrintLine
Here, the for structure automatically calls the next () method and assigns the return value of this method to line. The loop will end when stopiteration occurs.
Compared with sequences, loop objects are used to control loops. The benefit is that you do not need to generate the elements to be used each time the loop is not started. The elements used are generated in a loop. In this way, space is saved, efficiency is improved, and programming flexibility is improved.
2. ITER () function and iterator)
Technically, there is also an intermediate layer between the cyclic object and the for loop call, which is to convert the cyclic object into an iterator ). This conversion is implemented by using the ITER () function. But logically, this layer can be ignored, so the loop object and the loop operator often refer to each other.
3. Generator)
The main purpose of a generator is to construct a user-defined cyclic object.
The compilation method of the generator is similar to the function definition, but inPlace of returnChanged to yield.. The generator can have multiple yield instances. When the generator encounters a yield, it will pause running the generator and return the value following yield. When the generator is called again, it will continue running from the paused place until the next yield.The generator itself forms another iterator, Each cycle uses a value returned by yield.
The following is a generator:
DefGen ():= 100YieldA= A * 8YieldAYield1000
The generator has three yield instances. If it is used as a loop generator, it performs three cycles.
ForIInGen ():PrintI
Consider the following generator:
DefGen ():ForIInRange (4):YieldI
It can also be writtenGenerator expression):
G = (xForXInRange (4 ))
Generator expressions are a simple way to compile generators. For more information, see.
4. List Comprehension)
Table derivation is a quick way to generate tables. Suppose we generate table L:
L =[]ForXInRange (10): L. append (x** 2)
The preceding table L is generated, but there is actually a quick writing method, that isTable DerivationMethod:
L = [x ** 2ForXInRange (10)]
This is similar to the generator expression, except thatBrackets.
(The mechanism of table derivation is actually to use circular objects. If you are interested, you can refer to them .)
Consider what will be generated in the following Table derivation?
XL = [1, 3, 5] YL= [9, 12, 13] L= [X ** 2For(X, y)InZip (XL, yl)IfY> 10]
Summary:
Loop object
Generator
Table Derivation