Python traversal sequence enumerate function analysis, pythonenumerate

Source: Internet
Author: User

Python traversal sequence enumerate function analysis, pythonenumerate

The enumerate function is used to traverse the elements in the sequence and Their subscript.

Enumerate function description:

Function prototype: enumerate (sequence, [start = 0])

Function: lists the sequence data and data subscript respectively starting with start.

That is, for a data object that can be traversed (such as a list, tuples, or strings), enumerate combines the data object into an index sequence and lists both data and data subscript.

Example:

If a sequence exists and enumerate is used for it, the following result is obtained:

Start sequence [0]
Start + 1 sequence [1]
Start + 2 sequence [2] ......

Applicable version:

Python2.3 +
Python2.x

Note: The start parameter is added after python2.6.

Explanation:

Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. the next () method of the iterator returned by enumerate () returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence.

Code example:

The enumerate parameter is a variable that can be traversed, such as a string or a list. The returned value is the enumerate class.

import strings = string.ascii_lowercasee = enumerate(s)print sprint list(e)

Output:

abcdefghij[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'), (7, 'h'), (8, 'i'), (9, 'j')]

You can use enumerate when both the index and value values are required.

In this instance, line is a string that contains 0 and 1. We need to find all 1:

def xread_line(line): return((idx,int(val)) for idx, val in enumerate(line) if val != '0')print read_line('0001110101')print list(xread_line('0001110101'))

Summary

The above is an analysis of the python traversal sequence enumerate function provided by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.