在Pythonfor循环中如何获取循环次数?

在Python的for循环里,循环遍历可以写成:

for item in list:
    print item

它可以遍历列表中的所有元素,但是有什么方法可以知道到目前为止我循环了多少次?

想到的替代方案是:

count=0
for item in list:
    print item
    count +=1 if count % 10 == 0: print 'did ten'

或:

for count in range(0,len(list)): print list[count] if count % 10 == 0: print 'did ten'

有没有更好的方法(就像for item in list),可以统计到目前为止的循环次数?

猜你喜欢

转载自www.cnblogs.com/dingjiaoyang/p/10491600.html