Skip to content

Commit 6683b4b

Browse files
author
raymond.hettinger
committed
Clean-up itertools docs and recipes.
git-svn-id: http://svn.python.org/projects/python/trunk@65150 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 171d441 commit 6683b4b

File tree

2 files changed

+36
-101
lines changed

2 files changed

+36
-101
lines changed

Doc/library/itertools.rst

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,11 @@ equivalent result.
3535
Likewise, the functional tools are designed to work well with the high-speed
3636
functions provided by the :mod:`operator` module.
3737

38-
The module author welcomes suggestions for other basic building blocks to be
39-
added to future versions of the module.
40-
4138
Whether cast in pure python form or compiled code, tools that use iterators are
42-
more memory efficient (and faster) than their list based counterparts. Adopting
39+
more memory efficient (and often faster) than their list based counterparts. Adopting
4340
the principles of just-in-time manufacturing, they create data when and where
4441
needed instead of consuming memory with the computer equivalent of "inventory".
4542

46-
The performance advantage of iterators becomes more acute as the number of
47-
elements increases -- at some point, lists grow large enough to severely impact
48-
memory cache performance and start running slowly.
49-
5043

5144
.. seealso::
5245

@@ -598,55 +591,35 @@ which incur interpreter overhead.
598591

599592
.. testcode::
600593

601-
def take(n, seq):
602-
return list(islice(seq, n))
594+
def take(n, iterable):
595+
"Return first n items of the iterable as a list"
596+
return list(islice(iterable, n))
603597

604-
def enumerate(iterable):
605-
return izip(count(), iterable)
598+
def enumerate(iterable, start=0):
599+
return izip(count(start), iterable)
606600

607-
def tabulate(function):
601+
def tabulate(function, start=0):
608602
"Return function(0), function(1), ..."
609-
return imap(function, count())
610-
611-
def iteritems(mapping):
612-
return izip(mapping.iterkeys(), mapping.itervalues())
603+
return imap(function, count(start))
613604

614605
def nth(iterable, n):
615-
"Returns the nth item or raise StopIteration"
616-
return islice(iterable, n, None).next()
617-
618-
def all(seq, pred=None):
619-
"Returns True if pred(x) is true for every element in the iterable"
620-
for elem in ifilterfalse(pred, seq):
621-
return False
622-
return True
623-
624-
def any(seq, pred=None):
625-
"Returns True if pred(x) is true for at least one element in the iterable"
626-
for elem in ifilter(pred, seq):
627-
return True
628-
return False
629-
630-
def no(seq, pred=None):
631-
"Returns True if pred(x) is false for every element in the iterable"
632-
for elem in ifilter(pred, seq):
633-
return False
634-
return True
635-
636-
def quantify(seq, pred=None):
637-
"Count how many times the predicate is true in the sequence"
638-
return sum(imap(pred, seq))
639-
640-
def padnone(seq):
606+
"Returns the nth item or empty list"
607+
return list(islice(iterable, n, n+1))
608+
609+
def quantify(iterable, pred=bool):
610+
"Count how many times the predicate is true"
611+
return sum(imap(pred, iterable))
612+
613+
def padnone(iterable):
641614
"""Returns the sequence elements and then returns None indefinitely.
642615

643616
Useful for emulating the behavior of the built-in map() function.
644617
"""
645-
return chain(seq, repeat(None))
618+
return chain(iterable, repeat(None))
646619

647-
def ncycles(seq, n):
620+
def ncycles(iterable, n):
648621
"Returns the sequence elements n times"
649-
return chain.from_iterable(repeat(seq, n))
622+
return chain.from_iterable(repeat(iterable, n))
650623

651624
def dotproduct(vec1, vec2):
652625
return sum(imap(operator.mul, vec1, vec2))

Lib/test/test_itertools.py

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,52 +1185,32 @@ def __init__(self, newarg=None, *args):
11851185
[22]
11861186
[25, 26, 27, 28]
11871187
1188-
>>> def take(n, seq):
1189-
... return list(islice(seq, n))
1188+
>>> def take(n, iterable):
1189+
... "Return first n items of the iterable as a list"
1190+
... return list(islice(iterable, n))
11901191
1191-
>>> def enumerate(iterable):
1192-
... return izip(count(), iterable)
1192+
>>> def enumerate(iterable, start=0):
1193+
... return izip(count(start), iterable)
11931194
1194-
>>> def tabulate(function):
1195+
>>> def tabulate(function, start=0):
11951196
... "Return function(0), function(1), ..."
1196-
... return imap(function, count())
1197-
1198-
>>> def iteritems(mapping):
1199-
... return izip(mapping.iterkeys(), mapping.itervalues())
1197+
... return imap(function, count(start))
12001198
12011199
>>> def nth(iterable, n):
1202-
... "Returns the nth item"
1200+
... "Returns the nth item or empty list"
12031201
... return list(islice(iterable, n, n+1))
12041202
1205-
>>> def all(seq, pred=None):
1206-
... "Returns True if pred(x) is true for every element in the iterable"
1207-
... for elem in ifilterfalse(pred, seq):
1208-
... return False
1209-
... return True
1210-
1211-
>>> def any(seq, pred=None):
1212-
... "Returns True if pred(x) is true for at least one element in the iterable"
1213-
... for elem in ifilter(pred, seq):
1214-
... return True
1215-
... return False
1216-
1217-
>>> def no(seq, pred=None):
1218-
... "Returns True if pred(x) is false for every element in the iterable"
1219-
... for elem in ifilter(pred, seq):
1220-
... return False
1221-
... return True
1222-
1223-
>>> def quantify(seq, pred=None):
1224-
... "Count how many times the predicate is true in the sequence"
1225-
... return sum(imap(pred, seq))
1226-
1227-
>>> def padnone(seq):
1203+
>>> def quantify(iterable, pred=bool):
1204+
... "Count how many times the predicate is true"
1205+
... return sum(imap(pred, iterable))
1206+
1207+
>>> def padnone(iterable):
12281208
... "Returns the sequence elements and then returns None indefinitely"
1229-
... return chain(seq, repeat(None))
1209+
... return chain(iterable, repeat(None))
12301210
1231-
>>> def ncycles(seq, n):
1232-
... "Returns the sequence elements n times"
1233-
... return chain(*repeat(seq, n))
1211+
>>> def ncycles(iterable, n):
1212+
... "Returns the seqeuence elements n times"
1213+
... return chain(*repeat(iterable, n))
12341214
12351215
>>> def dotproduct(vec1, vec2):
12361216
... return sum(imap(operator.mul, vec1, vec2))
@@ -1315,24 +1295,6 @@ def __init__(self, newarg=None, *args):
13151295
>>> nth('abcde', 3)
13161296
['d']
13171297
1318-
>>> all([2, 4, 6, 8], lambda x: x%2==0)
1319-
True
1320-
1321-
>>> all([2, 3, 6, 8], lambda x: x%2==0)
1322-
False
1323-
1324-
>>> any([2, 4, 6, 8], lambda x: x%2==0)
1325-
True
1326-
1327-
>>> any([1, 3, 5, 9], lambda x: x%2==0,)
1328-
False
1329-
1330-
>>> no([1, 3, 5, 9], lambda x: x%2==0)
1331-
True
1332-
1333-
>>> no([1, 2, 5, 9], lambda x: x%2==0)
1334-
False
1335-
13361298
>>> quantify(xrange(99), lambda x: x%2==0)
13371299
50
13381300

0 commit comments

Comments
 (0)