Iteration count in python?

前端 未结 2 962
被撕碎了的回忆
被撕碎了的回忆 2021-01-11 11:38

Let\'s say I have a list of tuples l, and I do something like this:

for (a,b) in l:
     do something with a,b, and the index of (a,b) in l

相关标签:
2条回答
  • 2021-01-11 11:59

    Use enumerate():

    for i,(a,b) in enumerate(l):
       ... # `i` contains the index
    
    0 讨论(0)
  • 2021-01-11 12:04

    Use enumerate.

    for i, (a, b) in enumerate(l):
        # i will be the index of (a, b) in l
    
    0 讨论(0)
提交回复
热议问题