这对我来说不是一件小事,而且我找不到任何收据,所以也许你可以给我指一张,或者你有一个现成的、适当的、经过调整的解决方案?正确含义也适用于不知道自身长度的迭代器(没有__len__
),也适用于可穷尽迭代器(例如链式迭代器);经过良好调整的含义很快。在
注意:就地解决方案是不可能的,因为需要缓存迭代器的输出来重新迭代它们(Glenn Maynard指出了这一点)。在
用法示例:
>>> list(izip_cycle(range(2), range(5), range(3)))
[(0, 0, 0), (1, 1, 1), (0, 2, 2), (1, 3, 0), (0, 4, 1)]
>>> from itertools import islice, cycle, chain
>>> list(islice(izip_cycle(cycle(range(1)), chain(range(1), range(2))), 6))
[(0, 0), (0, 0), (0, 1), (0, 0), (0, 0), (0, 1)]
这里有一些灵感来自
itertools.tee
和itertools.cycle
。它适用于任何类型的iterable:根据您的要求,一种可能对您有用的简单方法是:
第一件事就是找出最长序列的长度,这样它就知道要重复多少次。没有
__len__
的序列被计算为长度为0;这可能是你想要的——如果你有一个没有结束的序列,你可能想在有限序列上重复。虽然这不能处理没有长度的有限迭代器。在我们永远不会使用^{} 创建每个迭代器的循环版本,然后使用^{} 将它们压缩在一起。在
最后,我们从zip中生成每个条目,直到给出所需数量的结果。在
如果您想让这对没有
^{pr2}$len
的有限迭代器有效,我们需要自己做更多的工作:这基本上是Python implementation of ^{} in the documentation 的扩展,可以在多个序列上运行。我们保存在
saved
中看到的项目来重复并跟踪exhausted
中哪些序列用完了。在因为这个版本会等待所有序列的运行,如果你传入无限的东西,循环将永远运行下去。在
相关问题 更多 >
编程相关推荐