[Python-checkins] r46427 - sandbox/trunk/Doc/functional.rst
andrew.kuchling
python-checkins at python.org
Sat May 27 02:32:41 CEST 2006
Author: andrew.kuchling
Date: Sat May 27 02:32:40 2006
New Revision: 46427
Modified:
sandbox/trunk/Doc/functional.rst
Log:
Describe more functions
Modified: sandbox/trunk/Doc/functional.rst
==============================================================================
--- sandbox/trunk/Doc/functional.rst (original)
+++ sandbox/trunk/Doc/functional.rst Sat May 27 02:32:40 2006
@@ -725,14 +725,54 @@
=>
/usr/bin/java, /bin/python, /usr/bin/perl, /usr/bin/ruby
+Another group of functions chooses a subset of an iterator's elements
+based on a **predicate**, a function that returns the truth value of
+some condition.
+
+``itertools.ifilter(predicate, iter)`` returns all the elements for
+which the predicate returns true::
+
+ def is_even(x):
+ return (x % 2) == 0
+
+ itertools.ifilter(is_even, itertools.count()) =>
+ 0, 2, 4, 6, 8, 10, 12, 14, ...
+
+``itertools.ifilterfalse(predicate, iter)`` is the opposite,
+returning all elements for which the predicate returns false::
+
+ itertools.ifilterfalse(is_even, itertools.count()) =>
+ 1, 3, 5, 7, 9, 11, 13, 15, ...
+
+``itertools.takewhile(predicate, iter)`` returns elements for as long
+as the predicate returns true. Once the predicate returns false,
+the iterator will signal the end of its results.
+
+::
+
+ def less_than_10(x):
+ return (x < 10)
+
+ itertools.takewhile(less_than_10, itertools.count()) =>
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+
+``itertools.dropwhile(predicate, iter)`` discards elements while the
+predicate returns true, and then returns the rest of the iterable's
+results.
+
+::
+
+ itertools.dropwhile(less_than_10, itertools.count()) =>
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...
+
+
+
+
+
.. comment
- ifilter
- ifilterfalse
- takewhile
- dropwhile
groupby
More information about the Python-checkins
mailing list