more fun with iterators (mux, demux)

Miles semanticist at gmail.com
Mon Apr 6 22:58:53 EDT 2009


On Mon, Apr 6, 2009 at 8:05 PM, Neal Becker wrote:
> I'm trying to make a multiplexor and demultiplexor, using generators.  The
> multiplexor will multiplex N sequences -> 1 sequence  (assume equal length).
> The demultiplexor will do the inverse.
>
> The demux has me stumped.  The demux should return a tuple of N generators.

from itertools import islice, tee

def demux(iterable, n):
    return tuple(islice(it, i, None, n) for (i, it) in
enumerate(tee(iterable, n)))

-Miles



More information about the Python-list mailing list