enumerate 使用

In [1]: enumerate?                                                              
Init signature: enumerate(self, /, *args, **kwargs)
Docstring:     
enumerate(iterable[, start]) -> iterator for index, value of iterable

Return an enumerate object.  iterable must be another object that supports
iteration.  The enumerate object yields pairs containing a count (from
start, which defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
    (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
Type:           type

In [2]: a = (1, 2, 3)                                                           

In [3]: for idx, value in enumerate(a): 
   ...:     print(idx, "|", value) 
   ...:                                                                         
0 | 1
1 | 2
2 | 3

In [4]: for idx, value in enumerate(a, 1): 
   ...:     print(idx, "|", value) 
   ...:                                                                         
1 | 1
2 | 2
3 | 3

In [5]: for idx, value in enumerate(a, 9): 
   ...:     print(idx, "|", value) 
   ...:                                                                         
9 | 1
10 | 2
11 | 3

In [6]:     

猜你喜欢

转载自blog.csdn.net/yjinyyzyq/article/details/89472180