summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-30 22:17:31 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-30 22:17:31 (GMT)
commita44327a9a226619a4eb0e658d30211baf920480b (patch)
treeca6a7d1fac6b31870093ddb8c8b40fb268e6cb83 /Doc/library
parent796fc3158572c9a20f64f6b1c54e278639e6f032 (diff)
downloadcpython-a44327a9a226619a4eb0e658d30211baf920480b.zip
cpython-a44327a9a226619a4eb0e658d30211baf920480b.tar.gz
cpython-a44327a9a226619a4eb0e658d30211baf920480b.tar.bz2
Update itertool recipes
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/itertools.rst24
1 files changed, 12 insertions, 12 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 05f1874..d8faba7 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -401,17 +401,6 @@ can be combined. ::
27
64
- >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura',
- ... '', 'martin', '', 'walter', '', 'mark']
- >>> for name in islice(reportlines, 3, None, 2):
- ... print name.title()
- ...
- Alex
- Laura
- Martin
- Walter
- Mark
-
# Show a dictionary sorted and grouped by value
>>> from operator import itemgetter
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
@@ -534,5 +523,16 @@ which incur interpreter overhead. ::
"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
-
+ def roundrobin(*iterables):
+ "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
+ # Recipe contributed by 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))