在 python 中,我该怎么做:
for car in cars:
# Skip first and last, do work for rest
其他答案仅适用于序列。
对于任何可迭代,跳过第一项:
itercars = iter(cars)
next(itercars)
for car in itercars:
# do work
如果你想跳过最后一个,你可以这样做:
itercars = iter(cars)
# add 'next(itercars)' here if you also want to skip the first
prev = next(itercars)
for car in itercars:
# do work on 'prev' not 'car'
# at end of loop:
prev = car
# now you can do whatever you want to do to the last one on 'prev'
这是一个更通用的生成器函数,它从可迭代的开头和结尾跳过任意数量的项目:
def skip(iterable, at_start=0, at_end=0):
it = iter(iterable)
for x in itertools.islice(it, at_start):
pass
queue = collections.deque(itertools.islice(it, at_end))
for x in it:
queue.append(x)
yield queue.popleft()
示例用法:
>>> list(skip(range(10), at_start=2, at_end=2))
[2, 3, 4, 5, 6, 7]
for item in list_name[1:-1]:
#...do whatever
例子:
mylist=['one','two','three','four','five']
for i in mylist[1:]:
print(i)
In python index start from 0, We can use slicing operator to make manipulations in iteration.
for i in range(1,-1):
这是我的首选。它不需要在循环中添加太多内容,并且只使用内置工具。
从...来:
for item in my_items:
do_something(item)
到:
for i, item in enumerate(my_items):
if i == 0:
continue
do_something(item)
好吧,您的语法一开始就不是真正的 Python。
Python 中的迭代在容器的内容之上(嗯,从技术上讲,它在迭代器之上),语法为for item in container
. 在这种情况下,容器是cars
列表,但您想跳过第一个和最后一个元素,这意味着cars[1:-1]
(python 列表是从零开始的,负数从末尾开始计数,并且:
是切片语法。
所以你要
for c in cars[1:-1]:
do something with c
基于@SvenMarnach 的答案,但更简单且不使用双端队列
>>> def skip(iterable, at_start=0, at_end=0):
it = iter(iterable)
it = itertools.islice(it, at_start, None)
it, it1 = itertools.tee(it)
it1 = itertools.islice(it1, at_end, None)
return (next(it) for _ in it1)
>>> list(skip(range(10), at_start=2, at_end=2))
[2, 3, 4, 5, 6, 7]
>>> list(skip(range(10), at_start=2, at_end=5))
[2, 3, 4]
另请注意,根据我的timeit
结果,这比双端队列解决方案略快
>>> iterable=xrange(1000)
>>> stmt1="""
def skip(iterable, at_start=0, at_end=0):
it = iter(iterable)
it = itertools.islice(it, at_start, None)
it, it1 = itertools.tee(it)
it1 = itertools.islice(it1, at_end, None)
return (next(it) for _ in it1)
list(skip(iterable,2,2))
"""
>>> stmt2="""
def skip(iterable, at_start=0, at_end=0):
it = iter(iterable)
for x in itertools.islice(it, at_start):
pass
queue = collections.deque(itertools.islice(it, at_end))
for x in it:
queue.append(x)
yield queue.popleft()
list(skip(iterable,2,2))
"""
>>> timeit.timeit(stmt = stmt1, setup='from __main__ import iterable, skip, itertools', number = 10000)
2.0313770640908047
>>> timeit.timeit(stmt = stmt2, setup='from __main__ import iterable, skip, itertools, collections', number = 10000)
2.9903135454296716
如果cars
是一个序列,你可以这样做
for car in cars[1:-1]:
pass
另一种方法:
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
该more_itertools
项目扩展itertools.islice
到处理负指数。
例子
import more_itertools as mit
iterable = 'ABCDEFGH'
list(mit.islice_extended(iterable, 1, -1))
# Out: ['B', 'C', 'D', 'E', 'F', 'G']
因此,您可以优雅地在可迭代的第一项和最后一项之间应用它切片元素:
for car in mit.islice_extended(cars, 1, -1):
# do something
我是这样做的,尽管它看起来像一个 hack,但它每次都有效:
ls_of_things = ['apple', 'car', 'truck', 'bike', 'banana']
first = 0
last = len(ls_of_things)
for items in ls_of_things:
if first == 0
first = first + 1
pass
elif first == last - 1:
break
else:
do_stuff
first = first + 1
pass