switch recipe?

Christian Tanzer tanzer at swing.co.at
Sat Jul 13 05:48:22 EDT 2002


"Mark McEahern" <marklists at mceahern.com> wrote:

> > So where I've ended up is a place I'm sure there's some fancy
> > name for--but I don't know it.
>
> The thread on zip suggests the term "lockstep iteration." I can't use
> map or zip though because I have an unbounded iterator.

Not so. As long as one of the iterators is finite, zip will work:

>>> from __future__ import generators
>>> def repeat(*args):
...     """Return a repeating alternator for args."""
...     while args:
...         for a in args:
...             yield a
...
>>> for i, j in zip(range(5), repeat("a","b","c")):
...   print i, j
...
0 a
1 b
2 c
3 a
4 b

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92






More information about the Python-list mailing list