简体   繁体   中英

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. 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,'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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM