我正在尝试使用 itertools.dropwhile
仅从生成器返回第三个元素之后的元素,但我遇到了一些麻烦:
from itertools import dropwhile
it = (i for i in range(10,20))
a = dropwhile(enumerate < 3, it)
next(a)
TypeError: 'bool' object is not callable
我正在寻找的输出是:
[14, 15, 16, 17, 18, 19]
谁能解释我的代码有什么问题并提供可行的解决方案?谢谢。
最佳答案
itertools
提供了一个功能,可以完全满足您的需求,甚至更多。来自 the Python Standard Library ,
itertools.
islice
(iterable[, start], stop[, step])Make an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Afterward, elements are returned consecutively unless step is set higher than one which results in items being skipped. If stop is
None
, then iteration continues until the iterator is exhausted, if at all; otherwise, it stops at the specified position.
>>> import itertools
>>> it = (i for i in range(10, 20)) # it = xrange(10, 20)
>>> a = itertools.islice(it, 4, None)
>>> list(a)
[14, 15, 16, 17, 18, 19]
关于python - itertools.dropwhile 有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081864/