在Python的for循环中获取时间循环

时间:2018-10-11 20:18:34

标签: python for-loop

我想获得我在for循环中循环的时间

for x in dictionary:
    print(timeslooped) # If the looped has looped 4 times then this should print 4

我该怎么做?

这是一个非常简单的问题,但是我最近使用了Python,但我不确定。谢谢!

2 个答案:

答案 0 :(得分:3)

您可以使用计数器:

timeslooped = 0
for x in dictionary:
    timeslooped +=1
    print(timeslooped) # If the looped has looped 4 times then this should print 4

或使用enumerate

for timeslooped, x in enumerate(dictionary): # enumerate returns the index and the value as a tuple
    print(timeslooped) 

答案 1 :(得分:2)

enumerate可以告诉您

for timeslooped, x in enumerate(dictionary):
    print(timeslooped+1)

添加了+1,因为它将从0开始而不是1