views:

76

answers:

1

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, ...
...

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)

but got this error:

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

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?

+2  A: 

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

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
THC4k