Python advanced 06 loop object

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.