Python's iterators and generators

Source: Internet
Author: User
Tags for in range

The generators and iterators in Python are easy to use, but the characteristics of the generators and iterators are not quite in place, and today the knowledge is collated.

Iterators

To better understand iterators and builds, we need to briefly review the concept of the iterator protocol.

Iterator protocol

1. An iterator protocol means that an object must provide a next method that either returns the next item in the iteration, or causes a Stopiteration exception to terminate the iteration (only backward cannot go forward)

2. An iterative object: An object that implements an iterator protocol (how to: Define an __iter__ () method within an object)

3. The Protocol is a convention that iterates over an object, implements an iterator protocol, and Python's internal tools (such as a For loop, Sum,min,max function, etc.) use an iterator protocol to access an object.

For loop

The nature of the For loop: Loop through all objects, all using an iterator protocol.

The For loop is based on an iterator protocol that provides a uniform way to traverse all objects, i.e., before iterating, invoking the object's __iter__ method to convert it to an iterator and then iterating through the iterator protocol so that all objects can be traversed by a for loop,

Lists, strings, tuples, dictionaries, collections, file objects, and so on are not inherently iterative objects, and in the case of a for loop, the internal _iter_ method is called first, making them an iterative object, then looping the elements in turn using the _next_ method of the Iterator object, The stopiteration exception is triggered when the element is cycled, and the for loop catches the exception and terminates the iteration.

If you visit a list, you can use the usual custom notation:

# For Loop Access # The for Loop l essence is to follow the access method of the iterator protocol, call the Diedai_l=l.__iter__ () method first, or direct diedai_l=iter (l), then execute Diedai_l.next () in turn, Until the for loop snaps to Stopiteration stop loop li = [1,2,3,4] for in Li:#li_iter = Li._iter_ ()    print(i)#li_iter._next_

You can also access it directly using iterators:

#Iterator Protocol AccessLi = [1,2,3,4]f= li.__iter__()#The first step is to first change the object into an iterative object by the internal _iter_ method.Print(F.__next__())#using the _next_ method to value an iterative objectPrint(F.__next__())Print(F.__next__())Print(F.__next__())Print(F.__next__())#stopiteration, out of bounds will be an error.
Generator

Before introducing the generator, let's briefly introduce the list-building

List-Generated

The list generation, which is the comprehensions, is a very simple and powerful build of Python built-in that can be used to create lists.

For example, to generate a list, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] you can use range(1, 11) :

>>> Range (1, one) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List generation can be lazy in programming instead of loops, such as [1x1, 2x2, 3x3, ..., 10x10] how to generate? You can use the normal loop, or you can do it with the List Builder, as follows:

 for  in range (1, one) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.

So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the Generator (Generator).

Two ways to create generators

The first method is simple, as long as a list of the generated formula is [] changed () to create a generator:

 for  in range () >>> 1, 4, 9, +, (+), +, +, Bayi]for in Range (Ten)>>> g<generator object <genexpr> at 0x104feab40>

Lis a list, and g is a generator, if you want to access the elements in the generator, you need to use the next () method of the generator. Or take advantage of a for loop, because generator is also an iterative object.

The second method requires "yield" to calculate the Fibonacci sequence as an example of how a function becomes a generator, directly on the code:

def fib (max):     = 0, 0, 1 while     n < Max:        print  b        = b, A + b        = n + 1

This is a normal function, and changing print to yield is the generator:

def fib (max):     = 0, 0, 1 while     n < Max:        yield  b        = b, A + b        = N + 1

The function is executed sequentially, the return statement is encountered, or the last line function statement is returned. The function that becomes generator, executes at each invocation next() , encounters a yield statement return, and executes again from the last statement returned yield . To give a simple example, define generator and return to 1,3,5:

>>>defOdd (): ...Print 'Step 1'...     yield1...     Print 'Step 2'...     yield3...     Print 'Step 3'...     yield5...>>> o =Odd ()>>>o.next () step11>>>o.next () step23>>>o.next () step35>>>O.next () Traceback (most recent): File"<stdin>", Line 1,inch<module>stopiteration

As you can see, odd it is not a normal function, but a generator, which is interrupted during execution and yield continues execution the next time. After executing 3 times yield , it has not been yield able to execute, so the 4th call will be an next() error.

Similarly, when you get an element, most of the time you use a for loop.

Reference: Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 00138681965108490cb4c13182e472f8d87830f13be6e88000

https://www.zhihu.com/question/20829330

Python's iterators and generators

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.