Unverified Kaydet (Commit) 337cbbac authored tarafından Raymond Hettinger's avatar Raymond Hettinger Kaydeden (comit) GitHub

Add comment and improve variable name in roundrobin() (#4486)

üst bc9b6e29
...@@ -753,15 +753,16 @@ which incur interpreter overhead. ...@@ -753,15 +753,16 @@ which incur interpreter overhead.
def roundrobin(*iterables): def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C" "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis # Recipe credited to George Sakkis
pending = len(iterables) num_active = len(iterables)
nexts = cycle(iter(it).__next__ for it in iterables) nexts = cycle(iter(it).__next__ for it in iterables)
while pending: while num_active:
try: try:
for next in nexts: for next in nexts:
yield next() yield next()
except StopIteration: except StopIteration:
pending -= 1 # Remove the iterator we just exhausted from the cycle.
nexts = cycle(islice(nexts, pending)) num_active -= 1
nexts = cycle(islice(nexts, num_active))
def partition(pred, iterable): def partition(pred, iterable):
'Use a predicate to partition entries into false entries and true entries' 'Use a predicate to partition entries into false entries and true entries'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment