Count indexes using “for” in Python

后端 未结 7 1299
清歌不尽
清歌不尽 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:11

    The most simple way I would be going for is;

    i = -1
    for step in my_list:
        i += 1
        print(i)
    
    #OR - WE CAN CHANGE THE ORDER OF EXECUTION - SEEMS MORE REASONABLE
    
    i = 0
    for step in my_list:
        print(i) #DO SOMETHING THEN INCREASE "i"
        i += 1
    

提交回复
热议问题