计算迭代次数 [英] Counting iterations
问题描述
有没有更好的方法来计算迭代次数?:
宠物= 0
我的宠物:
宠物+ = 1
打印宠物 +# +宠物
谢谢,
Derek Basch
Is there a better way to count iterations that this?:
pets = 0
for i in pets:
pets += 1
print "pet" + "#" + pets
Thanks,
Derek Basch
推荐答案
Derek Basch写道:
Derek Basch wrote:
有没有更好的方法来计算这个迭代?:宠物= 0
我的宠物:
宠物+ = 1
打印宠物 +# +宠物
Is there a better way to count iterations that this?:
pets = 0
for i in pets:
pets += 1
print "pet" + "#" + pets
for i,宠物的枚举(宠物):
打印''宠物#%i''%(i + 1 )
STeVe
for i, pet in enumerate(pets):
print ''pet#%i'' % (i + 1)
STeVe
Derek Basch写道:
Derek Basch wrote:
有没有更好的方法来计算迭代次数这个?:
宠物= 0
我的宠物:
宠物+ = 1
打印宠物 +# +宠物
Is there a better way to count iterations that this?:
pets = 0
for i in pets:
pets += 1
print "pet" + "#" + pets
您可以使用''enumerate''获取索引,但上面的代码不起作用 -
您正试图迭代一个非序列。
将McGugan
-
"" .join( [{''@'':''@'',''。'':''。''}。get(c,None)或chr(97 +((ord(c)-97)+13) %26)
for c injv ** @ jvyyzpthtna.pbz])
You can use ''enumerate'' to get the index, but the code above wont work -
you are trying to iterate over a non-sequence.
Will McGugan
--
"".join( [ {''@'':''@'',''.'':''.''}.get(c,None) or chr(97+((ord(c)-97)+13)%26)
for c in "jv**@jvyyzpthtna.pbz" ] )
ooops你是对的。应该是:
pets = [" cat",dog,bird]
num_pets = 0
for i in pet:
num_pets + = 1
print" pet" +# + num_pets
这就是一次性问题。我不读它们:)。
ooops you are right. Should have been:
pets = ["cat", "dog", "bird"]
num_pets = 0
for i in pets:
num_pets += 1
print "pet" + "#" + num_pets
That''s the problem with one offs. I don''t read them :).
这篇关于计算迭代次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!