Count indexes using “for” in Python

后端 未结 7 1317
清歌不尽
清歌不尽 2021-02-01 01:44

I need to do in Python the same as:

for (i = 0; i < 5; i++) {cout << i;} 

but I don\'t know how to use FOR in Python to get the index

7条回答
  •  花落未央
    2021-02-01 02:01

    use enumerate:

    >>> l = ['a', 'b', 'c', 'd']
    >>> for index, val in enumerate(l):
    ...    print "%d: %s" % (index, val)
    ... 
    0: a
    1: b
    2: c
    3: d
    

提交回复
热议问题