diff options
author | Raymond Hettinger <python@rcn.com> | 2003-08-25 05:06:09 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-08-25 05:06:09 (GMT) |
commit | 9e3864190c1552e44da181fc8bee903348fdc51c (patch) | |
tree | 3fe093b77b45acec3545a4c70ff9550d0994c351 /Doc | |
parent | 9bd308f67b476e72319e22490992b648b3461845 (diff) | |
download | cpython-9e3864190c1552e44da181fc8bee903348fdc51c.zip cpython-9e3864190c1552e44da181fc8bee903348fdc51c.tar.gz cpython-9e3864190c1552e44da181fc8bee903348fdc51c.tar.bz2 |
Make the derived tools amendable to cut and paste.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/lib/libitertools.tex | 108 |
1 files changed, 54 insertions, 54 deletions
diff --git a/Doc/lib/libitertools.tex b/Doc/lib/libitertools.tex index cd7e6de..7fdd602 100644 --- a/Doc/lib/libitertools.tex +++ b/Doc/lib/libitertools.tex @@ -328,59 +328,59 @@ included here to illustrate how higher level tools can be created from building blocks. \begin{verbatim} ->>> def enumerate(iterable): -... return izip(count(), iterable) - ->>> def tabulate(function): -... "Return function(0), function(1), ..." -... return imap(function, count()) - ->>> def iteritems(mapping): -... return izip(mapping.iterkeys(), mapping.itervalues()) - ->>> def nth(iterable, n): -... "Returns the nth item" -... return list(islice(iterable, n, n+1)) - ->>> def all(pred, seq): -... "Returns True if pred(x) is True for every element in the iterable" -... return False not in imap(pred, seq) - ->>> def some(pred, seq): -... "Returns True if pred(x) is True at least one element in the iterable" -... return True in imap(pred, seq) - ->>> def no(pred, seq): -... "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)) - ->>> def ncycles(seq, n): -... "Returns the sequence elements n times" -... return chain(*repeat(seq, n)) - ->>> def dotproduct(vec1, vec2): -... return sum(imap(operator.mul, vec1, vec2)) - ->>> def window(seq, n=2): -... "Returns a sliding window (of width n) over data from the iterable" -... " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... " -... it = iter(seq) -... result = tuple(islice(it, n)) -... if len(result) == n: -... yield result -... for elem in it: -... result = result[1:] + (elem,) -... yield result - ->>> def take(n, seq): -... return list(islice(seq, n)) +def enumerate(iterable): + return izip(count(), iterable) + +def tabulate(function): + "Return function(0), function(1), ..." + return imap(function, count()) + +def iteritems(mapping): + return izip(mapping.iterkeys(), mapping.itervalues()) + +def nth(iterable, n): + "Returns the nth item" + return list(islice(iterable, n, n+1)) + +def all(pred, seq): + "Returns True if pred(x) is True for every element in the iterable" + return False not in imap(pred, seq) + +def some(pred, seq): + "Returns True if pred(x) is True at least one element in the iterable" + return True in imap(pred, seq) + +def no(pred, seq): + "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)) + +def ncycles(seq, n): + "Returns the sequence elements n times" + return chain(*repeat(seq, n)) + +def dotproduct(vec1, vec2): + return sum(imap(operator.mul, vec1, vec2)) + +def window(seq, n=2): + "Returns a sliding window (of width n) over data from the iterable" + " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... " + it = iter(seq) + result = tuple(islice(it, n)) + if len(result) == n: + yield result + for elem in it: + result = result[1:] + (elem,) + yield result + +def take(n, seq): + return list(islice(seq, n)) \end{verbatim} |