Python内置函数之enumerate() 函数

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中

  • for循环使用enumerate示例1
>>>seq = ['one', 'two', 'three']
>>>for temp in enumerate(seq):
>>>    print(temp)
    
(0, 'one')
(1, 'two')
(2, 'three')
  • for循环使用enumerate示例2
>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
...     print (i, element)
... 
0 one
1 two
2 three
posted @ 2022-12-12 13:39  辛宣  阅读(25)  评论(0)    收藏  举报