enumerate() 函數用於將一個可遍歷的數據對象(如列表、元組或字符串)組合爲一個索引序列,同時列出數據和數據下標,一般用在 for 循環當中。
>>>seq = ['one', 'two', 'three']
>>>for i, element in enumerate(seq):
... print(i, seq[i])
...
0 one
1 two
2 three
>>>
enumerate() 函數用於將一個可遍歷的數據對象(如列表、元組或字符串)組合爲一個索引序列,同時列出數據和數據下標,一般用在 for 循環當中。
>>>seq = ['one', 'two', 'three']
>>>for i, element in enumerate(seq):
... print(i, seq[i])
...
0 one
1 two
2 three
>>>