Python iterators and generators

Source: Internet
Author: User

Python iterators and generators

Iterators

Iterators in Python provide the class sequence object (Sequence-like objects) with an interface to a class sequence that iterates not only over a Sequence object (string, list, tuple), but also on a sequence, But the object that shows the sequence behavior iterates, such as the dictionary key, the line of the file.

The iterator object has a next () method that returns the next entry after the call. After all entries have been iterated, the iterator throws a stopiteration exception telling the program that the loop is over. The For statement can be used for a sequence type, or for an iterator type, which calls next () internally and catches an exception.

However, iterators have some limitations that cannot be moved backwards, cannot go back to the beginning, and cannot replicate an iterator .

The ITER (obj) factory function can return an iterator, and the reversed () function returns an iterator that accesses the reverse order.

For example:

' XYZ ', 45.67= iter (t)    #  The IT = reversed (t) function returns an iterator to the iterated access it.next ()       # 123It.next ()       #  ' xyz 'it.next ()       #  45.67it.next ()       #  stopiteration

Another way to use the ITER () method is ITER (func, Sentinel), which repeatedly calls Func until the next value of the iterator equals Sentinel.

def foo ():     Global a     Print (a)     + = 1    return= iter (foo, One-for-in it:      Pass

In the example above, the for loop will stop at i=11.

We know that the For statement accepts an iterative object (a sequence or an iterator) as its argument, iterating over one of the elements at a time. It automatically calls the iterator's next () method, captures the stopiteration exception, and ends the loop, all of which occur internally. For example:

 for inch " Hello World " Print C

The above is an iterative element through a sequence item, and here is a method for iterating through a sequence index using the range function.

The Range function returns a list of three invocation forms:

#Range (end) #  start=0, Step=1

For example:

 for  in range (2, 3):    print"", Eachval

Note that when there is a very wide list, xrange () may be more suitable because it does not create a full copy of the list in memory, it can only be used in a for loop, and it does not make sense to use it outside of the for loop.

In addition, xrange () returns a Xrange object, which is neither a Sequence object nor an iterator object.

The usage of xrange () is exactly the same as range ():

 for  in Xrange (2, 3):    print"", Eachval

But Xrange does not return a list, but returns a value for Each loop, which is better, and later describes the generator expression, which is a similar principle.

Also, do not modify objects when they are iterated.

LST = range (+)print(LST) for in lst:    = ele*3Print (LST)

It can be found that LST has not changed.

If you want to modify an object for an iteration, you can use an index iteration:

LST = rangeprint(LST) for in range (len (LST)):    = lst[k]*3print(LST)

List parsing

Syntax for list parsing:

[Expr for Iter_var in iterable if COND_EXPR]

Returns a list.

For example:

seq = [1, 2, 3, 4, 5, 6, 7, 8, 9]         for inif item%2]      #  [1, 3, 5, 7, 9]

Another example is to print a 3*5 matrix:

 for inch  for  in range (5)]

Builder expression

The generator expression, like the use of list parsing, simply replaces "[]" with "()", but the generator expression returns not a list, but the generator (generator), which is essentially a function.

After each calculation of an entry, the generator yields the entry, which uses a lazy evaluation, so it is more efficient in memory.

For example:

>>> matrix = ((x+1, y+1) forXinchRange (3) forYinchRange (5))   >>> forIinchMatrixPrint(i) ... (1, 1)(1, 2)(1, 3)(1, 4)(1, 5)(2, 1)(2, 2)(2, 3)(2, 4)(2, 5)(3, 1)(3, 2)(3, 3)(3, 4)(3, 5)

For example, if you want a large text file with the longest line:

 for  in open ('/etc/motd'))

If you use list parsing here, it is inevitable that all the rows of the entire file will have to be loaded into memory, but that the generator expression will be more performance-bound.

Generator

The essence of the generator is a function with a yield statement, usually a function can only be returned once, and the generator can pause execution and return an intermediate result (the function of the yield statement), return a value to the caller and pause execution. When the next () method of the generator is called, it will proceed exactly from where it left off.

As you can see, the generator is similar to the concept of a co-process, pausing or suspending, and continuing execution from where the program left off.

rows = [1, 2, 3, +]def  cols ():    yield ,    yield , 2    yield 1 for as in cols ())  x_product_pairs:    print(pair)

Operation Result:

(1, About)(1,2)(1,1)(2, About)(2,2)(2,1)(3, About)(3,2)(3,1)( -, About)( -,2)( -,1)

When the next () method of the generator is called, the generator executes until the yield statement appears and returns the yield argument to the caller (similar to return).

Note that the code that follows the yield statement no longer runs. It is necessary to continue calling the next () method the next time until the function exits.

In addition, the caller can return the value to the generator (via the Send () method) and require the generator to exit (Close ()).

Example:

defCounter (start_at=0): Count=Start_at whileTrue:val= (yieldcount)ifVal is  notNone:Print("Val is not None", Val) Count=ValElse: Count+ = 1C= Counter (5)Print(C.next ())#5Print(C.next ())#6Print(C.next ())#7C.send (100)Print(C.next ())#101c.close () c.next ( )#stopiteration

The generator has an initialized value, and each time the generator next () is called to Count 1, the user can reset the value by Send (), and the Close () method is called to terminate the generator.

Python 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.