iteration. Iterator. Builder. Python

Source: Internet
Author: User
Tags iterable

Iteration

  Given a list or tuple, we can iterate through for  the list or tuple through a loop, This traversal is called iterative (iteration).

Iterative: A sequence of objects, physical, or virtual storage.

    List,tuple,strings,dictionary,set and generator objects are iterative, and integer types are not iterative. So, how can you tell if an object is an iterative object? The method is judged by the iterable type of the collections module:

    

" in Python, you can use the type () and isinstance () functions to determine the object type. 】

Or is:

    

iter () returns the listiterator object for the list , returning the dictionary-keyiterator for dictionary Object. Similarly, an iterator object is returned for other types of iterations.

The last small question, what if I want to implement a Java -like subscript loop on the list ? Python 's built-in enumerate   function allows you to turn a list into an index-element pair so that you can for  iterate over both the index and the element in the loop Itself:

    

Iterators

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

You can use isinstance () to determine whether an object is a Iterator object:

  

Generators are Iterator objects, but list dict str  , although iterable, they are not Iterator.

Iterable, and so on into Iterator can use iter()  the function: list dict str 

  

Generator

  In Python , a mechanism that loops one side of the computation, called the generator:generator.

Create a generator:

  

The difference between creation and the outermost []  and () is a list, and is a generator.

"generator is the algorithm "

iteration. Iterator. Builder. Python

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.