python - itertools.dropwhile 有困难

标签 python python-itertools

我正在尝试使用 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/

相关文章:

Python列表理解,可以用于读取多个文件吗?

Python itertools 分组

python - 为什么笛卡尔积产生 "TypeError: iteration over a 0-d array"?

python - 按升序对月份名称字符串列表进行排序

python - 通过python中出现零来选择列表中的元素

python - 自动将文件分发给用户

python - 罗莎琳德溶液固定: shared motifs

python - 如何将 Python 的 itertools.product 库从列表理解转换为普通的 for 循环?

python - Flask Nginx Guincorn/uwsgi docker 配置

python - 尝试在 PostgresQL SELECT 中使用 Python 变量时的语法问题