Python 生成器将另一个可迭代分为 N 组
我正在寻找一个函数,它接受可迭代的 i
和大小 n
并生成长度为 n
的元组,这些元组是来自 的顺序值>i
:
x = [1,2,3,4,5,6,7,8,9,0]
[z for z in TheFunc(x,3)]
给出
[(1,2,3),(4,5,6),(7,8,9),(0)]
标准库中是否存在这样的函数?
如果它作为标准库的一部分存在,我似乎无法找到它,并且我已经没有要搜索的术语了。我可以自己写,但我不愿意。
I'm looking for a function that takes an iterable i
and a size n
and yields tuples of length n
that are sequential values from i
:
x = [1,2,3,4,5,6,7,8,9,0]
[z for z in TheFunc(x,3)]
gives
[(1,2,3),(4,5,6),(7,8,9),(0)]
Does such a function exist in the standard library?
If it exists as part of the standard library, I can't seem to find it and I've run out of terms to search for. I could write my own, but I'd rather not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当您想要将迭代器分成
n
块进行分组时,不使用填充值填充最终组,请使用iter(lambda: list(IT. islice(iterable, n)), [])
:yields
这个答案。
当您想要将迭代器分成
n
块并用填充值填充最终组时,请使用 石斑鱼食谱zip_longest(*[iterator]*n)
:例如,在Python2:
在 Python3 中,
izip_longest
现已重命名为zip_longest
:当您想要将序列分组为
块时n
你可以使用chunks
配方:请注意,与一般的迭代器不同,根据定义的序列具有长度(即定义了
__len__
)。When you want to group an iterator in chunks of
n
without padding the final group with a fill value, useiter(lambda: list(IT.islice(iterable, n)), [])
:yields
There is an explanation of how it works in the second half of this answer.
When you want to group an iterator in chunks of
n
and pad the final group with a fill value, use the grouper recipezip_longest(*[iterator]*n)
:For example, in Python2:
In Python3, what was
izip_longest
is now renamedzip_longest
:When you want to group a sequence in chunks of
n
you can use thechunks
recipe:Note that, unlike iterators in general, sequences by definition have a length (i.e.
__len__
is defined).请参阅 itertoolsgrouper 配方> package
(但是,这是相当多的问题的重复。)
See the
grouper
recipe in the docs for theitertools
package(However, this is a duplicate of quite a few questions.)
这个怎么样?但它没有填充值。
它使用原始可迭代对象的副本,并为每个连续的拼接耗尽该副本。我疲惫的大脑能想到的唯一其他方法是生成具有范围的拼接端点。
也许我应该将
list()
更改为tuple()
,以便它更好地对应您的输出。How about this one? It doesn't have a fill value though.
It utilizes a copy of the original iterable, which it exhausts for each successive splice. The only other way my tired brain could come up with was generating splice end-points with range.
Maybe I should change
list()
totuple()
so it better corresponds to your output.这是 Python 中非常常见的请求。它非常常见,以至于它被纳入 boltons 统一实用程序包中。首先,这里有大量文档。此外,模块的设计和测试仅依赖于标准库(Python 2 和 3 兼容),这意味着您可以 只需下载该文件直接进入您的项目。
还有用于不定/长序列的迭代器/生成器形式:
如您所见,您也可以用您选择的值填充序列。最后,作为维护者,我可以向您保证,虽然代码已被数千名开发人员下载/测试,但如果您遇到任何问题,您将通过 boltons GitHub 问题页面。希望这个(和/或任何其他 150 多个 Bolton 食谱)有所帮助!
This is a very common request in Python. Common enough that it made it into the boltons unified utility package. First off, there are extensive docs here. Furthermore, the module is designed and tested to only rely on the standard library (Python 2 and 3 compatible), meaning you can just download the file directly into your project.
There's an iterator/generator form for indefinite/long sequences as well:
As you can see, you can also fill the sequence with a value of your choosing, as well. Finally, as the maintainer, I can assure you that, while the code has been downloaded/tested by thousands of developers, if you encounter any issues, you'll get the fastest support possible through the boltons GitHub Issues page. Hope this (and/or any of the other 150+ boltons recipes) helped!
我使用 more_itertools 包中的分块函数。
I use the chunked function from the more_itertools package.
这是一个非常古老的问题,但我认为针对一般情况提及以下方法是有用的。它的主要优点是它只需要迭代数据一次,因此它可以与数据库游标或其他只能使用一次的序列一起使用。我还发现它更具可读性。
This is a very old quesiton, but I think it is useful to mention the following approach for the general case. Its main merit is that it only needs to iterate over the data once, so it will work with database cursors or other sequences that can only be used once. I also find it more readable.
我知道这个问题已经被回答过好几次了,但我添加了我的解决方案,与石斑鱼配方相比,它应该在序列和迭代器的一般适用性、可读性(没有 StopIteration 异常导致的不可见循环退出条件)和性能方面得到改进。它与 Svein 的最后一个答案最相似。
I know this has been answered several times but I'm adding my solution which should improve in both, general applicability to sequences and iterators, readability (no invisible loop exit condition by StopIteration exception) and performance when compared to the grouper recipe. It is most similar to the last answer by Svein.
这是一个不同的解决方案,它不使用 itertools,尽管它多了几行,但当块比可迭代长度短得多时,它显然优于给定的答案。
然而,对于大块,其他答案要快得多。
Here is a different solution which makes no use of itertools and, even though it has a couple more lines, it apparently outperforms the given answers when chunks are a lot shorter than the iterable lenght.
However, for big chunks the other answers are much faster.