首页 > 应用开发 > Python > Pythonfor循环中获取循环次数的方法
在Python的
for循环里,循环遍历可以写成:
for item in list:print item
它可以遍历列表中的所有元素,但是有什么方法可以知道到目前为止我循环了多少次?
想到的替代方案是:
count=0for item in list:print itemcount +=1if 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),可以统计到目前为止的循环次数?
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31556183/viewspace-2669563/ ,如需转载,请注明出处,否则将追究法律责任。