Identify which iteration you are on in a loop in python

前端 未结 5 578
走了就别回头了
走了就别回头了 2020-12-11 15:33

Basically I would like to be able to tell when I\'m on the Nth item in a loop iteration. Any thoughts?

d = {1:2, 3:4, 5:6, 7:8, 9:0}

for x in d:
    if last         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 16:20

    list = [1,2,3]
    
    last = list[-1]
    
    for i in list:
        if i == last:
            print("Last:")
        print i
    

    Output:

    1
    2
    Last:
    3
    

提交回复
热议问题