This function converts an iterative object to an enumeration object. iterable is an iterative parameter, such as a list, an array, a dictionary, and other objects; Start is the starting value of the enumeration, which is the default from 0 start. The principle of this function is to obtain a value from the method __next__ () of the iterative object, and then Start counting the parameter start, adding 1 per item , a tuple is generated to return.
This function implements the principle, basically can use the following code to represent:
def enumerate (sequence, start=0):
n = Start
For Elem in sequence:
Yield N, Elem
n + = 1
Let's take a look at the example below:
#enumerate () L = [' A ', ' B ', ' C ']print (List (Enumerate (L))) T = (' happy ', ' happy ', ' happy ') print (list (enumerate (t))) d = {' Shenzhen ': 1, ' Guangzhou ': 2, ' Zhuhai ': 3}print (List (Enumerate (d, 2))) s = ' Shenzhen is a high-tech super city with a population of 18 million people ' Print (list (enumerate (S, 1)))
The resulting output is as follows:
[(0, ' a '), (1, ' B '), (2, ' C ')]
[(0, ' happy ' ), (1, ' happy ') , (2, ' happy ')]
[(2, ' Shenzhen ' ), (3, ' Zhuhai '), (4, ' guangzhou ')]
[(1, 'Deep'), (2, 'Chun'), (3, 'is a'), (4, 'a'), (5, 'a'), (6, 'High'), (7, 'Section'), (8, 'Technology'), (9, 'of the'), (Ten, 'Super'), (one, '-Level'), (City'), (inCity'), (+, ','), (people'), (+, 'Mouth'), (+, 'hold'), (have a'), (+, ' 1 ') , (+, ' 8 '), (+, ' 0 '), (+, ' 0 '), (atmillion'), (people')]
Cai Junsheng qq:9073204 Shenzhen
Python standard library: Built-in function enumerate (iterable, start=0)