[Tutor] What is this an example of (and how can i use it?)
kevin parks
kp8 at mac.com
Sun Sep 20 15:10:10 CEST 2009
I am afraid that in the long layoff in python has meant some new
constructs have passed me by. In googling around I found some nice
little code I want to use, but i don't quite understand it, how it is
called, and what it is an example of. I guess there are generators and
iterators now and it seems this might be an example of one of those
new constructs. Can anyone explain what it is i am looking at, how it
is called, and what it is an example of so that I can look it up:
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).next for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
More information about the Tutor
mailing list