复制可迭代对象的Python方法 [英] Pythonic way of copying an iterable object

查看:91
本文介绍了复制可迭代对象的Python方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个我正在从事的小项目,我需要循环浏览一个列表。对于该循环中的每个元素,我都必须从同一列表开始另一个循环,将前一个元素作为新循环的第一个元素。例如,我希望能够生成如下内容:

For a small project I'm working on I need to cycle through a list. For each element of this cycle I have to start another cycle through the same list, with the former element as first element of the new cycle. For example I'd like to be able to produce something like this:

1, 2, 3, 4, 1, 2, 3, 4, 1, ...
2, 3, 4, 1, 2, 3, 4, 1, 2, ...
3, 4, 1, 2, 3, 4, 1, 2, 3, ...
4, 1, 2, 3, 4, 1, 2, 3, 4, ...
1, 2, 3, 4, 1, 2, 3, 4, 1, ...
...

我以为复制每个.next()之后的itertools.cycle将保留当前状态,因此我可以使用外部循环中的元素开始新的循环。甚至重置循环指针到较旧的位置。我尝试了以下操作:

I thought that copying a itertools.cycle after each .next() would conserve the current state, so that I can begin the new cycle with the element from the "outer" cycle. Or even "reset the cycle pointer" to an older position. I tried the following:

>>> import itertools, copy
>>> a = itertools.cycle([1, 2, 3, 4])
>>> b = copy.copy(a)

但出现此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/copy.py", line 95, in copy
    return _reconstruct(x, rv, 0)
  File "/usr/lib/python2.6/copy.py", line 323, in _reconstruct
    y = callable(*args)
  File "/usr/lib/python2.6/copy_reg.py", line 93, in __newobj__
    return cls.__new__(cls, *args)
TypeError: cycle expected 1 arguments, got 0

我知道有很多方法可以实现我想要的,但是我正在寻找一些简短,清晰和Pythonic的代码。也许有人有其他想法,甚至是摘要? 无法复制迭代器对象的事实我的兴趣。在需要可迭代对象的副本的情况下,是否存在最佳实践?还是一般来说,复制可迭代项是愚蠢且无用的?

I know there are many different ways to achieve what I want but I'm looking for some short, clear and pythonic code. Maybe someone has another idea or even a snippet? The fact that it's not possible to copy iterator objects woke my interest. Is there a best-practice in situations where one wants a copy of an iterable? Or is copying iterables silly and useless in general?

推荐答案


在某些情况下是否有最佳实践
哪里需要可迭代的副本?

Is there a best-practice in situations where one wants a copy of an iterable?

itertools.tee 为您提供了两个迭代器,每个迭代器与原始迭代器产生相同的项目,但是它使用原始迭代器并记住其产生的所有内容,因此您无法再使用原始迭代器。不过,这在这里无济于事,因为它会一直存储这些循环值,直到出现MemoryError。

itertools.tee gives you two iterators that each yield the same items as the original, but it takes the original and memorizes everything it yields, so you can't use the original anymore. It wouldn't help here though, because it would keep on memorizing these cycled values until you get a MemoryError.


或者正在愚蠢地复制可迭代对象和
一般没用吗?

Or is copying iterables silly and useless in general?

迭代器只是被定义为具有当前状态并产生一个项目。您无法确定它们将来会产生相同的物料还是过去产生的物料。真正的副本必须同时做这两个事情,所以这是不可能的!

iterators are just defined to have a current state and yield a item. You can't tell if they will yield the same items in the future or which items they yielded in the past. A real copy would have to do both, so it's impossible!

在您的情况下,制作一个新的周期太简单了,我宁愿这样做而不是尝试复制现有的。例如:

In your case it's so trivial to make a new cycle that I'd rather do that than try to copy an existing. For example:

def new_cycle( seq, last=None):
    if last is None:
        return cycle(seq)
    else:
        it = cycle(seq)
        while next(it) != last:
            pass
        return it

这篇关于复制可迭代对象的Python方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆