Python bit record 9: an iterative approach

Source: Internet
Author: User

iteration, in a simple way, is to access each element of the list individually in some order.

The For statement is a relatively simple way to iterate over time:

Lis = [' A ', ' h ', ' A ', ' I ', ' I ']for x in Lis:print x

This is accessed once for each element in the list.

In addition to the for statement, there is a way to implement the iteration, which is the ITER () method:

Lis = [' A ', ' h ', ' A ', ' I ', ' i ']lis_iter = iter (LIS) Lis_iter.next ()

Lis_iter.next () reads only one element of the list at a time, and then reads the next element when it is executed again. The error occurs until there is no element at the end of the read. Because the pointer does not automatically return to the start position at the end of execution, the next time you use the Iter_iter (). Next () method iterations, the iteration object needs to be re-loaded, which requires re-execution:

Lis_iter = iter (LIS)


Python bit record 9: an iterative approach

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.