Iterators and ITER () functions

Source: Internet
Author: User

1, first to an example, there is a preliminary impression:

myTuple=(123,‘xyz‘,45.67)i=iter(myTuple)i.next()123i.next()‘xyz‘i.next()45.67i.nextcall""1in?StopIteration

The above code explicitly uses the iterator through the ITER () function, and the iterator is a class containing the next function, and
Usually we are using such as:

forin seq:    do_something_to(i)

Also used in the iterator, the actual working code should be as follows:

fetch=iter(seq)whileTrue:    try:        i = fetch.next()    except StopIteration:        break    do_something_to(i)

And, normally we should use iterators in the same way as above to wrap iterators in try...except ... In
2. Type of Use
In addition to tuples, lists, dictionaries, files, etc. can be used,
such as dictionary type

forin myDict:    do_something_to(i)

The dictionary iterator iterates through its keys
3. Precautions
when iterating over mutable objects, do not attempt to modify them.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Iterators and ITER () functions

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.