more fun with iterators (mux, demux)

Neal Becker ndbecker2 at gmail.com
Mon Apr 6 20:05:51 EDT 2009


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 mux seems easy enough:

-----------------------
def mux (*ranges):
    iterables = [iter (r) for r in ranges]
    while (True):
        for i in (iterables):
            yield i.next()

def test_mux ():
    a = xrange (10)
    b = xrange (10,20)
    return mux (a, b)

x = test_mux()
print [e for e in x]
------------------------

The demux has me stumped.  The demux should return a tuple of N generators.  
The signature should be:

def demux (sequence, N):

Not sure about this one.






More information about the Python-list mailing list