diff options
author | Raymond Hettinger <python@rcn.com> | 2003-08-08 02:40:28 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-08-08 02:40:28 (GMT) |
commit | c7d7766fda5d08f00ac4d46c5a9d5d4361e8038d (patch) | |
tree | 3c6c8427f442a7aebdfbafce3a59e2371f1300bf /Doc | |
parent | f5c96fb74d86e844af564acdc54ce3f1e70df56c (diff) | |
download | cpython-c7d7766fda5d08f00ac4d46c5a9d5d4361e8038d.zip cpython-c7d7766fda5d08f00ac4d46c5a9d5d4361e8038d.tar.gz cpython-c7d7766fda5d08f00ac4d46c5a9d5d4361e8038d.tar.bz2 |
Improve docs:
* Simplify the pure python examples
* Add a quantify() example
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/lib/libitertools.tex | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/Doc/lib/libitertools.tex b/Doc/lib/libitertools.tex index d17fc6f..b708050 100644 --- a/Doc/lib/libitertools.tex +++ b/Doc/lib/libitertools.tex @@ -78,7 +78,7 @@ by functions or loops that truncate the stream. Make an iterator that returns consecutive integers starting with \var{n}. Does not currently support python long integers. Often used as an argument to \function{imap()} to generate consecutive data points. - Also, used in \function{izip()} to add sequence numbers. Equivalent to: + Also, used with \function{izip()} to add sequence numbers. Equivalent to: \begin{verbatim} def count(n=0): @@ -103,9 +103,7 @@ by functions or loops that truncate the stream. for element in iterable: yield element saved.append(element) - if len(saved) == 0: - return - while True: + while saved: for element in saved: yield element \end{verbatim} @@ -124,13 +122,12 @@ by functions or loops that truncate the stream. \begin{verbatim} def dropwhile(predicate, iterable): iterable = iter(iterable) - while True: - x = iterable.next() - if predicate(x): continue # drop when predicate is true + for x in iterable: + if not predicate(x): + yield x + break + for x in iterable: yield x - break - while True: - yield iterable.next() \end{verbatim} \end{funcdesc} @@ -209,9 +206,7 @@ by functions or loops that truncate the stream. \begin{verbatim} def islice(iterable, *args): s = slice(*args) - next = s.start or 0 - stop = s.stop - step = s.step or 1 + next, stop, step = s.start or 0, s.stop, s.step or 1 for cnt, element in enumerate(iterable): if cnt < next: continue @@ -278,9 +273,7 @@ by functions or loops that truncate the stream. \begin{verbatim} def takewhile(predicate, iterable): - iterable = iter(iterable) - while True: - x = iterable.next() + for x in iterable: if predicate(x): yield x else: @@ -358,6 +351,10 @@ from building blocks. ... "Returns True if pred(x) is False for every element in the iterable" ... return True not in imap(pred, seq) +>>> def quantify(pred, seq): +... "Count how many times the predicate is True in the sequence" +... return sum(imap(pred, seq)) + >>> def padnone(seq): ... "Returns the sequence elements and then returns None indefinitely" ... return chain(seq, repeat(None)) |