python enumerate

list1 = ['a', 'b', 'c']


i = 0
for item in list1:
    print(i, list1[i])
    i += 1


for i, item in enumerate(list1):
    print(i, item)
0 a
1 b
2 c
0 a
1 b
2 c

可以看到比普通的for循环方便不少
http://www.waitingfy.com/archives/3770

3770

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.