Skip first entry in for loop in python?

前端 未结 13 2461
面向向阳花
面向向阳花 2020-12-02 05:15

In python, How do I do something like:

for car in cars:
   # Skip first and last, do work for rest
13条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 06:05

    An alternative method:

    for idx, car in enumerate(cars):
        # Skip first line.
        if not idx:
            continue
        # Skip last line.
        if idx + 1 == len(cars):
            continue
        # Real code here.
        print car
    

提交回复
热议问题