Python enumerate function code parsing, 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 all about parsing the enumerate function code in Python. I hope it will be helpful to you. Interested friends can continue to refer to this site: python data type to determine the difference between type and isinstance instance parsing, python in the requests library session Object Wonderful Details and so on, more exciting content, all in http://www.bkjia.com /!