Python 3.x study notes 5 (iterator and generator), python3.x

Source: Internet
Author: User
Tags iterable

Python 3.x study notes 5 (iterator and generator), python3.x

1. Iterator ):
An object that can be called by the next () function and continuously returns the next value to become an Iterator: Iterator
Objects that can be directly used for a for loop are collectively referred to as iteration objects: Iterable

Iteration, as its name implies, is to repeat some things many times (as we do in the current loop ). An iterator is an object that implements the _ next _ () method (this method does not require any parameters during calling). It is a way to access the iterated sequence, generally, it is accessed from the first element of the sequence until all elements are accessed. However, the iterator can only forward and not backward.

Advantages:
Using the iterator does not require that all elements in the entire iteration process be prepared in advance. The iterator calculates the element only when it iterates to an element. Before or after this, the element may not exist or be destroyed. Therefore, the iterator is suitable for traversing a large or even infinite number of sequences.

2. Generator:
Only the corresponding data is generated during the call, and there is only one _ next _ () method.

If the list elements can be calculated by some algorithm, can we continue to calculate the subsequent elements in the loop process? In this way, you do not need to create a complete list to save a lot of space. In Python, this type of computing mechanism is called a Generator ).

Creation method:
1) The simplest is to change [] of a list generation type to (), and a generator is created.

2) You can change the function to generator object. You only need to change print () to yield.


3.

Yeild Function: Save the current status and return
Send:Wake up and pass a value
_ Next:Just wake up


4.

Generators are all Iterator objects. However, although list, non-productive field, and str are Iterable, they are not Iterator. You can use the iter () function to convert Iterable such as list, dict, and str into Iterator.

 

5. Use yield to convert the Fibonacci function into a generator.

 

1 def fib (max): 2 n, a, B = 0, 0, 1 3 while n <max: 4 yield B # convert the function into generator 5 a, B = B, a + B 6 n + = 1 7 return 'lalalalal' 8 9 f = fib (10) 10 11 while True: 12 try13 x = next (f) # It is equivalent to executing the extract function fib () 14 print (x) 15 minute t StopIteration as e: 16 print ('generator return value is % s' % e. value) # e's value is the function's return value, such as the fib function's return value 'lalalalal' 17 break

 

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.