首頁  >  文章  >  後端開發  >  Python中記錄循環次數的方法

Python中記錄循環次數的方法

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼原創
2019-06-15 17:35:2412844瀏覽

Python中如何記錄循環次數?

相關推薦:《python影片

Python中記錄循環次數的方法

在Python的for迴圈裡,循環遍歷可以寫成:

for item in list:
    print item

它可以遍歷列表中的所有元素,但是有什麼方法可以知道到目前為止我循環了多少次?

想到的替代方案是:

count=0for 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'

以上是Python中記錄循環次數的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn