Add examples.
This commit is contained in:
parent
4291b19d4e
commit
040f10e9b3
|
@ -71,6 +71,7 @@ loops that truncate the stream.
|
|||
Equivalent to::
|
||||
|
||||
def chain(*iterables):
|
||||
# chain('ABC', 'DEF') --> A B C D E F
|
||||
for it in iterables:
|
||||
for element in it:
|
||||
yield element
|
||||
|
@ -83,6 +84,7 @@ loops that truncate the stream.
|
|||
|
||||
@classmethod
|
||||
def from_iterable(iterables):
|
||||
# chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
|
||||
for it in iterables:
|
||||
for element in it:
|
||||
yield element
|
||||
|
@ -108,7 +110,8 @@ loops that truncate the stream.
|
|||
Equivalent to::
|
||||
|
||||
def combinations(iterable, r):
|
||||
'combinations(range(4), 3) --> (0,1,2) (0,1,3) (0,2,3) (1,2,3)'
|
||||
# combinations('ABCD', 2) --> AB AC AD BC BD CD
|
||||
# combinations(range(4), 3) --> 012 013 023 123
|
||||
pool = tuple(iterable)
|
||||
n = len(pool)
|
||||
indices = range(r)
|
||||
|
@ -145,6 +148,7 @@ loops that truncate the stream.
|
|||
numbers. Equivalent to::
|
||||
|
||||
def count(n=0):
|
||||
# count(10) --> 10 11 12 13 14 ...
|
||||
while True:
|
||||
yield n
|
||||
n += 1
|
||||
|
@ -157,6 +161,7 @@ loops that truncate the stream.
|
|||
indefinitely. Equivalent to::
|
||||
|
||||
def cycle(iterable):
|
||||
# cycle('ABCD') --> A B C D A B C D A B C D ...
|
||||
saved = []
|
||||
for element in iterable:
|
||||
yield element
|
||||
|
@ -177,6 +182,7 @@ loops that truncate the stream.
|
|||
start-up time. Equivalent to::
|
||||
|
||||
def dropwhile(predicate, iterable):
|
||||
# dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
|
||||
iterable = iter(iterable)
|
||||
for x in iterable:
|
||||
if not predicate(x):
|
||||
|
@ -215,6 +221,8 @@ loops that truncate the stream.
|
|||
:func:`groupby` is equivalent to::
|
||||
|
||||
class groupby(object):
|
||||
# [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
|
||||
# [(list(g)) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
|
||||
def __init__(self, iterable, key=None):
|
||||
if key is None:
|
||||
key = lambda x: x
|
||||
|
@ -245,6 +253,7 @@ loops that truncate the stream.
|
|||
that are true. Equivalent to::
|
||||
|
||||
def ifilter(predicate, iterable):
|
||||
# ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9
|
||||
if predicate is None:
|
||||
predicate = bool
|
||||
for x in iterable:
|
||||
|
@ -259,6 +268,7 @@ loops that truncate the stream.
|
|||
that are false. Equivalent to::
|
||||
|
||||
def ifilterfalse(predicate, iterable):
|
||||
# ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
|
||||
if predicate is None:
|
||||
predicate = bool
|
||||
for x in iterable:
|
||||
|
@ -277,6 +287,7 @@ loops that truncate the stream.
|
|||
useful way of supplying arguments to :func:`imap`. Equivalent to::
|
||||
|
||||
def imap(function, *iterables):
|
||||
# imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000
|
||||
iterables = map(iter, iterables)
|
||||
while True:
|
||||
args = [it.next() for it in iterables]
|
||||
|
@ -299,6 +310,10 @@ loops that truncate the stream.
|
|||
multi-line report may list a name field on every third line). Equivalent to::
|
||||
|
||||
def islice(iterable, *args):
|
||||
# islice('ABCDEFG', 2) --> A B
|
||||
# islice('ABCDEFG', 2, 4) --> C D
|
||||
# islice('ABCDEFG', 2, None) --> C D E F G
|
||||
# islice('ABCDEFG', 0, None, 2) --> A C E G
|
||||
s = slice(*args)
|
||||
it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
|
||||
nexti = it.next()
|
||||
|
@ -321,6 +336,7 @@ loops that truncate the stream.
|
|||
lock-step iteration over several iterables at a time. Equivalent to::
|
||||
|
||||
def izip(*iterables):
|
||||
# izip('ABCD', 'xy') --> Ax By
|
||||
iterables = map(iter, iterables)
|
||||
while iterables:
|
||||
result = [it.next() for it in iterables]
|
||||
|
@ -346,6 +362,7 @@ loops that truncate the stream.
|
|||
Iteration continues until the longest iterable is exhausted. Equivalent to::
|
||||
|
||||
def izip_longest(*args, **kwds):
|
||||
# izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
|
||||
fillvalue = kwds.get('fillvalue')
|
||||
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
|
||||
yield counter() # yields the fillvalue, or raises IndexError
|
||||
|
@ -382,7 +399,8 @@ loops that truncate the stream.
|
|||
Equivalent to::
|
||||
|
||||
def permutations(iterable, r=None):
|
||||
'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)'
|
||||
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
|
||||
# permutations(range(3)) --> 012 021 102 120 201 210
|
||||
pool = tuple(iterable)
|
||||
n = len(pool)
|
||||
r = n if r is None else r
|
||||
|
@ -424,8 +442,8 @@ loops that truncate the stream.
|
|||
Equivalent to nested for-loops in a generator expression. For example,
|
||||
``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
|
||||
|
||||
The leftmost iterators are in the outermost for-loop, so the output tuples
|
||||
cycle like an odometer (with the rightmost element changing on every
|
||||
The leftmost iterators correspond to the outermost for-loop, so the output
|
||||
tuples cycle like an odometer (with the rightmost element changing on every
|
||||
iteration). This results in a lexicographic ordering so that if the
|
||||
inputs iterables are sorted, the product tuples are emitted
|
||||
in sorted order.
|
||||
|
@ -438,6 +456,8 @@ loops that truncate the stream.
|
|||
actual implementation does not build up intermediate results in memory::
|
||||
|
||||
def product(*args, **kwds):
|
||||
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
|
||||
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
|
||||
pools = map(tuple, args) * kwds.get('repeat', 1)
|
||||
result = [[]]
|
||||
for pool in pools:
|
||||
|
@ -451,10 +471,11 @@ loops that truncate the stream.
|
|||
|
||||
Make an iterator that returns *object* over and over again. Runs indefinitely
|
||||
unless the *times* argument is specified. Used as argument to :func:`imap` for
|
||||
invariant parameters to the called function. Also used with :func:`izip` to
|
||||
create an invariant part of a tuple record. Equivalent to::
|
||||
invariant function parameters. Also used with :func:`izip` to create constant
|
||||
fields in a tuple record. Equivalent to::
|
||||
|
||||
def repeat(object, times=None):
|
||||
# repeat(10, 3) --> 10 10 10
|
||||
if times is None:
|
||||
while True:
|
||||
yield object
|
||||
|
@ -472,6 +493,7 @@ loops that truncate the stream.
|
|||
between ``function(a,b)`` and ``function(*c)``. Equivalent to::
|
||||
|
||||
def starmap(function, iterable):
|
||||
# starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
|
||||
for args in iterable:
|
||||
yield function(*args)
|
||||
|
||||
|
@ -485,6 +507,7 @@ loops that truncate the stream.
|
|||
predicate is true. Equivalent to::
|
||||
|
||||
def takewhile(predicate, iterable):
|
||||
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
|
||||
for x in iterable:
|
||||
if predicate(x):
|
||||
yield x
|
||||
|
@ -528,23 +551,6 @@ Examples
|
|||
The following examples show common uses for each tool and demonstrate ways they
|
||||
can be combined. ::
|
||||
|
||||
>>> amounts = [120.15, 764.05, 823.14]
|
||||
>>> for checknum, amount in izip(count(1200), amounts):
|
||||
... print 'Check %d is for $%.2f' % (checknum, amount)
|
||||
...
|
||||
Check 1200 is for $120.15
|
||||
Check 1201 is for $764.05
|
||||
Check 1202 is for $823.14
|
||||
|
||||
>>> import operator
|
||||
>>> for cube in imap(operator.pow, xrange(1,5), repeat(3)):
|
||||
... print cube
|
||||
...
|
||||
1
|
||||
8
|
||||
27
|
||||
64
|
||||
|
||||
# Show a dictionary sorted and grouped by value
|
||||
>>> from operator import itemgetter
|
||||
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
|
||||
|
|
Loading…
Reference in New Issue