Re: Split iterator into multiple streams

2010-11-06 Thread Arnaud Delobelle
Steven D'Aprano writes: > Suppose I have an iterator that yields tuples of N items (a, b, ... n). > > I want to split this into N independent iterators: > > iter1 -> a, a2, a3, ... > iter2 -> b, b2, b3, ... > ... > iterN -> n, n2, n3, ... > > The iterator may be infinite, or at least too big to c

Re: Split iterator into multiple streams

2010-11-06 Thread Paul Rubin
Steven D'Aprano writes: > def split(iterable, n): > iterators = [] > for i, iterator in enumerate(itertools.tee(iterable, n)): > f = lambda it, i=i: (t[i] for t in it) > iterators.append(f(iterator)) > return tuple(iterators) > > Is this the right approach, or have I mi

Re: Split iterator into multiple streams

2010-11-06 Thread Peter Otten
Steven D'Aprano wrote: > Suppose I have an iterator that yields tuples of N items (a, b, ... n). > > I want to split this into N independent iterators: > > iter1 -> a, a2, a3, ... > iter2 -> b, b2, b3, ... > ... > iterN -> n, n2, n3, ... > > The iterator may be infinite, or at least too big to

Re: Split iterator into multiple streams

2010-11-06 Thread Raymond Hettinger
On Nov 6, 1:52 am, Steven D'Aprano wrote: > I tried changing the t[i] to use operator.itergetter instead, but no > luck. Finally I got this: > > def split(iterable, n): >     iterators = [] >     for i, iterator in enumerate(itertools.tee(iterable, n)): >         f = lambda it, i=i: (t[i] for t in

Re: Split iterator into multiple streams

2010-11-06 Thread Ian
On Nov 6, 2:52 am, Steven D'Aprano wrote: > My first attempt was this: > > def split(iterable, n): >     iterators = [] >     for i, iterator in enumerate(itertools.tee(iterable, n)): >         iterators.append((t[i] for t in iterator)) >     return tuple(iterators) > > But it doesn't work, as all

Split iterator into multiple streams

2010-11-06 Thread Steven D'Aprano
Suppose I have an iterator that yields tuples of N items (a, b, ... n). I want to split this into N independent iterators: iter1 -> a, a2, a3, ... iter2 -> b, b2, b3, ... ... iterN -> n, n2, n3, ... The iterator may be infinite, or at least too big to collect in a list. My first attempt was thi