python-iterator detailed

Source: Internet
Author: User

Originally in the sketch generator, but the generator is a special kind of iterator, so I think the iterator should be written first.

What is an iteration

An object that can directly act on a for loop is called an iterative object (iterable).

An object that can be called by the next () function and continually returns the next value is called an iterator (Iterator).

All iterable can be converted to iterator via the built-in function iter ()

Iteration Type :

1,for loop: Used for any object that can be iterated, in fact, in Python, the iterative tools that scan objects from left to right, and so on, include for loops, list parsing, in-member relationship testing, and map built-in functions.

2. File iterator: Because the file object is its own iterator, the advantage is: (Reduce the use of memory, do not need to load the content into memory)

3, Manual iteration: ITER and Next

At the start of the For loop, it is passed to the ITER built-in function to get a hit from an iterative object, and the returned object contains the required next method (this step is not necessary for the file because the file object is its own iterator)

For example:

#文件 >>> Import tab>>> f=open ("test", "R") >>> ITER (f) is ftrue# sequence string >>> a=[1,2,3,4] >>> ITER (a) is Afalse

Liefs

This article is from the "Wang Xiao Acid" blog, please be sure to keep this source http://wangaimin.blog.51cto.com/8499946/1908361

python-iterator detailed

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.