Python standard library: built-in function enumerate (iterable, start = 0), enumerateiterable
This function converts an Iterated object to an enumerated object. Iterable is an iterative parameter, such as a list, array, or dictionary object. start is the start value of enumeration. The default value is from 0. The implementation principle of this function is as follows: Get a value from the method _ next _ () of the iteration object, and then count the parameter start. each item is increased by 1, generate a tuple and return it.
The implementation principle of this function can be expressed in the following code:
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 'print (list (enumerate (s, 1 )))
The output is as follows:
[(0, 'A'), (1, 'B'), (2, 'C')]
[(0, 'happy '), (1, 'happy'), (2, 'happy ')]
[(2, 'shenzhen '), (3, 'zhuhai'), (4, 'guangzhou ')]
[(1, 'shen '), (2, 'shen'), (3, 'Yes'), (4, '1'), (5, 'tag'), (6, 'high'), (7, 'core'), (8, 'tech'), (9, '), (10, 'super'), (11, 'level'), (12, 'city'), (13, 'city'), (14, ','), (15, people), (16, 'login'), (17, 'login'), (18, 'you'), (19, '1'), (20, '8'), (21, '0'), (22, '0'), (23, 'wan'), (24, 'people')]
Cai junsheng QQ: 9073204 Shenzhen