[英]How to enumerate starting with custom number
Using: 使用:
myList=['One','Two','Three'[
for i,each in enumerate(myList):
print i,each
results to: 结果为:
0,'One'
1,'Two'
2,'Three'
As we can see enumerate starts iterating with i=0, then 1, then 2 and so one. 如我们所见,枚举从i = 0开始迭代,然后依次为1、2和1。 What if I would like to start with other than zero value, lets say it want it to be 5. So the result would be:
如果我想从零值开始,该怎么办呢?假设它是5。结果是:
5,'One'
6,'Two'
7,'Three'
Is it doable? 可以吗
将start=5
传递给enumerate()
:
for i,each in enumerate(myList, start=5):
Just pass in the starting number: 只需输入起始号码:
for i,each in enumerate(myList,5): # <- start at 5
myList=['One','Two','Three']
for i,each in enumerate(myList,5):
print i,each
5 One
6 Two
7 Three
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.