Python3 iterators and generators

Source: Internet
Author: User

Edit


Note content: Python3 iterators and generators
Note Date: 2017-10-28

Iterators

Iteration is one of the most powerful features of Python and is a way to access the elements of a collection.
An iterator is an object that remembers where to traverse.
The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without backing back.
Iterators have two basic methods: ITER () and next ().
A string, list, or tuple object can be used to create an iterator that creates an iterator code example for a list:

List1 = [Three, five, five, four, 910]it = iter (list1) #通过iter方法创建迭代器对象 # Start the iteration from the first element, and next returns the element of the iteration print (next (IT)) print (Next (IT)) Print (Next (IT)) print (Next (IT)) print (Next (IT))

Operation Result:

12
34
56
78
910

An iterator object can be traversed using a regular for statement, without having to write next:

List1 = [the] 910]it = ITER (list1) for Elenmet in It:print (Elenmet, end= "")

Operation Result:

12 34 56) 78 910

You can also use the next () function:

List1 = [the] 910]it = ITER (list1) for Elenmet in It:print (Elenmet, end= "")

Operation Result:

12 34 56) 78 910


Generator

In Python, a function that uses yield is called a generator (generator).
Unlike a normal function, a generator is a function that returns an iterator that can be used only for iterative operations, and simpler to understand that the generator is an iterator.
In the process of calling the generator to run, the function pauses and saves all current run information each time the yield is encountered, returning the value of yield. and continue running from the current location the next time the next () method is executed.
The following example uses yield to implement the Fibonacci sequence:

Import SYS # introduces SYS module DEF Fibonacci (n): # generator Function-Fibonacci A, B, counter = 0, 1, 0while true:if (Counter > N): Returnyield AA , B = B, a + bcounter + = 1f = Fibonacci (Ten) # F is an iterator object that is returned by the generator while True:try:print (Next (f), end= "") except Stopiterati On:sys.exit () #当异常发生就退出解释器

Operation Result:

0 1 1 2 3 5 8 13 21 34 55



This article is from the "zero" blog, make sure to keep this source http://zero01.blog.51cto.com/12831981/1977055

Python3 iterators and generators

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.