ITPub博客

首页 > 应用开发 > Python > Pythonfor循环中获取循环次数的方法

Pythonfor循环中获取循环次数的方法

Python 作者:daksio 时间:2019-12-19 19:19:38 0 删除 编辑

在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/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-09-26

  • 博文量
    6
  • 访问量
    20330