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!