python中enumerate方法,返回一個enumerate類型。參數通常是能夠遍歷的的東西,好比列表,字符串什麼的。python
python文檔中是這麼說的:ide
enumerate(sequence, [start=0])函數
Return an enumerate object. sequence must be a sequence, an iterator, or some other object which sup-spa
ports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing文檔
a count (from start which defaults to 0) and the corresponding value obtained from iterating over iter-字符串
able. enumerate() is useful for obtaining an indexed series: (0, seq[0]), (1, seq[1]), (2,it
seq[2]), .... io
For example:class
>>> for i, season in enumerate([’Spring’, ’Summer’, ’Fall’, ’Winter’]):object
...
print i, season
0 Spring
1 Summer
2 Fall
3 Winter
我的理解是:當你既須要下標,又須要內容時能夠用這個函數來解決
如下是我寫的例子:
# 字符串的使用 value_1 = 'fdahkjlzkjfhaqf' index = 0 for i in value_1: # 不使用enumerate函數 print index, i index += 1 for index, value in enumerate(value_1): # 使用enumerate函數 print index, value # 列表的使用 value_2 = ['a', 'b', 'c', 'd'] index = 0 for i in value_2: # 不使用enumerate函數 print index, i index += 1 for index, value in enumerate(value_2): # 使用enumerate函數 print index, value