How to output an index while iterating over an array in python

This question already has an answer here:

  • Accessing the index in 'for' loops? 17 answers

  • 喜欢这个:

    for index, g in enumerate(games[0:4]):
        g.output(index)
    

    使用内置的enumerate方法:

    for i,a in enumerate(['cat', 'dog']):
       print '%s is %d' % (a, i)
    
    # output:
    # cat is 0
    # dog is 1
    
    链接地址: http://www.djcxy.com/p/24004.html

    上一篇: 如何获得迭代器对象的索引?

    下一篇: 如何在python中迭代数组时输出索引