我想获得我在for循环中循环的时间
for x in dictionary:
print(timeslooped) # If the looped has looped 4 times then this should print 4
我该怎么做?
这是一个非常简单的问题,但是我最近使用了Python,但我不确定。谢谢!
答案 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