switch recipe?

Fredrik Lundh fredrik at pythonware.com
Sat Jul 13 08:37:47 EDT 2002


Mark McEahern

> The thread on zip suggests the term "lockstep iteration."  I can't use map
> or zip though because I have an unbounded iterator.  Of course, I could
> easily bound that to the length of the finite iterator.

zip stops as soon as the first sequence runs out of items:

class over_and_over_again:
    def __init__(self, seq):
        self.seq = seq
        self.len = len(seq)
    def __getitem__(self, index):
        return self.seq[index % self.len]

print zip(range(12), over_and_over_again("spam"))

prints

[(0, 's'), (1, 'p'), (2, 'a'), (3, 'm'), (4, 's'), (5, 'p'),
(6, 'a'), (7, 'm'), (8, 's'), (9, 'p'), (10, 'a'), (11, 'm')]

</F>





More information about the Python-list mailing list