Introduction to Python iterator and generator, python Generator

Source: Internet
Author: User

Introduction to Python iterator and generator, python Generator

Iterator

The iterator is an object that implements the iterator protocol. In Python, The iterator Protocol means that objects with the next method will move forward to the next result. At the end of a series of results, the StopIteration is triggered.

In the for loop, Python will automatically call the factory function iter () to obtain the iterator, call next () to obtain the element, and check for StopIteration exceptions.

Several commonly used built-in data structures, such as tuple, list, set, and dict, support iterators. character strings can also be iterated.

You can also implement an iterator by yourself. As mentioned above, you only need to return an object in the _ iter _ method of the class. This object has a next () method, this method can throw a StopIteration exception when appropriate. However, there is not much time to implement the iterator by yourself. It is easier to use the generator even if needed.

Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding = UTF-8

Class test:
Def _ init _ (self, input_list ):
Self. list = input_list
Self. I = 0

Def _ iter _ (self ):
Return self

Def next (self ):
If self. I = len (self. list ):
Self. I = 0
Raise StopIteration
Self. I + = 1
Return self. list [self. I-1]

An obvious advantage of using the iterator is that reading only one piece of data from the object at a time does not cause excessive memory overhead.

For example:

Copy codeThe Code is as follows:
/* Load the file into the memory once and print it row by row. When the file size is large, the memory overhead of this method is very high */
For line in open ("test.txt"). readlines ():
Print line

/* This is the simplest and fastest way to run. Instead of explicitly reading the file, it uses the iterator to read the next line each time */
For line in open ("test.txt"): # use file iterators
Print line

Generator

The Writing Method of the generator is similar to the function definition, but the return is changed 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 an iterator that uses a value returned by yield for each iteration.

Note that the return statement is not required in the generator, and the return value does not need to be specified. The default return statement already exists in the generator.

Generator expression
Copy codeThe Code is as follows:
(I for I in range (5 ))
// Return iterator
<Generator object <genexpr> at 0x7ff3e8f0d960>

List parsing, returns list
Copy codeThe Code is as follows:
[I for I in range (5)]
// Return list
[0, 1, 2, 3, 4]

There is a problem here, that is, range (5) will return a data with a length of 5. If it is range (1000), it will occupy an array space of 1000; if we use the 'generator 'to generate a number as needed, the space usage will be reduced. here we can use the xrange () function.
 Copy codeThe Code is as follows:
'''
Xrange
Function Description: The usage is exactly the same as that of range. The difference is that not an array, but a generator.
Xrange example:
'''
>>> Xrange (5)
Xrange (5)
>>> List (xrange (5 ))
[0, 1, 2, 3, 4]
>>> Xrange (1, 5)
Xrange (1, 5)
>>> List (xrange (1, 5 ))
[1, 2, 3, 4]
>>> Xrange (0, 6, 2)
Xrange (0, 6, 2)
>>> List (xrange (0, 6, 2 ))
[0, 2, 4]

Therefore, xrange performs a loop better than range, especially when a large value is returned, use xrange whenever possible, unless you want to return a list.

Related Article

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.