Get loop count inside a Python FOR loop

后端 未结 5 999
故里飘歌
故里飘歌 2020-11-28 00:42

In a Python for loop that iterates over a list we can write:

for item in list:
    print item

and it neatly goes through all t

5条回答
  •  佛祖请我去吃肉
    2020-11-28 01:20

    Agree with Nick. Here is more elaborated code.

    #count=0
    for idx, item in enumerate(list):
        print item
        #count +=1
        #if count % 10 == 0:
        if (idx+1) % 10 == 0:
            print 'did ten'
    

    I have commented out the count variable in your code.

提交回复
热议问题