Another itertool function?
vze4rx4y at verizon.net
Tue Apr 29 08:37:52 CEST 2003
[Magnus Lie Hetland]
> The need for the following function just cropped up in a parser I'm
> writing; it seems sufficiently general to perhaps be a candidate for
> itertools -- and I can't see any way of implementing it with the
> current itertools functions:
Okay, I tried it and the result was horrible:
"Works perfectly except fails to terminate"
return ifilter(None, imap(type(chain()).next,
cycle(imap(chain, iterables, repeat(repeat(None))))))
> def weave(*iterables):
> iterables = map(iter, iterables)
> while iterables:
> for it in iterables[:]:
> yield it.next()
> except StopIteration:
> Another possible name (which is a bit more unwieldy) would be
I find this name to be the most descriptive.
> I expect there would have to be more use-cases to justify it, but I
> just thought I'd air the idea...
Probably there should be more use cases.
It does seem to be a general purpose tool
for combining iterators.
Here's another idea that was submitted:
def windowed(iterable, group=2):
"returns (i, i, ... i[group-1]), (i, i, ... i[group]), ..."
it = iter(iterable)
result = 
if len(result) == group:
More information about the Python-list