diff --git a/Doc/lib/libitertools.tex b/Doc/lib/libitertools.tex index cd7e6de8fa1..7fdd602514d 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 enumerate(iterable): + return izip(count(), iterable) ->>> def tabulate(function): -... "Return function(0), function(1), ..." -... return imap(function, count()) +def tabulate(function): + "Return function(0), function(1), ..." + return imap(function, count()) ->>> def iteritems(mapping): -... return izip(mapping.iterkeys(), mapping.itervalues()) +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 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 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 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 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 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 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 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 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 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 take(n, seq): + return list(islice(seq, n)) \end{verbatim}