find position of item in for loop over a sequence
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Accessing the index in Python for loops
1 2 3 4 | list = [1,2,2,3,5,5,6,7] for item in mylist: ... |
在循环中的某一点上,如何找到正在查看的项的索引?我可以看到
看看枚举
1 2 3 4 5 6 | >>> for i, season in enumerate('Spring Summer Fall Winter'.split(), start=1): print i, season 1 Spring 2 Summer 3 Fall 4 Winter |
使用枚举器对象:
1 2 | for index, item in enumerate(mylist): ... |