35
35
Likewise, the functional tools are designed to work well with the high-speed
36
36
functions provided by the :mod:`operator` module.
38
The module author welcomes suggestions for other basic building blocks to be
39
added to future versions of the module.
41
38
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
43
40
the principles of just-in-time manufacturing, they create data when and where
44
41
needed instead of consuming memory with the computer equivalent of "inventory".
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.
375
368
except IndexError:
378
If one of the iterables is potentially infinite, then the :func:`izip_longest`
379
function should be wrapped with something that limits the number of calls (for
380
example :func:`islice` or :func:`takewhile`).
371
If one of the iterables is potentially infinite, then the
372
:func:`izip_longest` function should be wrapped with something that limits
373
the number of calls (for example :func:`islice` or :func:`takewhile`). If
374
not specified, *fillvalue* defaults to ``None``.
382
376
.. versionadded:: 2.6
602
return list(islice(seq, n))
604
def enumerate(iterable):
605
return izip(count(), iterable)
607
def tabulate(function):
595
def take(n, iterable):
596
"Return first n items of the iterable as a list"
597
return list(islice(iterable, n))
599
def enumerate(iterable, start=0):
600
return izip(count(start), iterable)
602
def tabulate(function, start=0):
608
603
"Return function(0), function(1), ..."
609
return imap(function, count())
611
def iteritems(mapping):
612
return izip(mapping.iterkeys(), mapping.itervalues())
604
return imap(function, count(start))
614
606
def nth(iterable, n):
615
"Returns the nth item or raise StopIteration"
616
return islice(iterable, n, None).next()
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):
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):
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):
636
def quantify(seq, pred=None):
637
"Count how many times the predicate is true in the sequence"
638
return sum(imap(pred, seq))
607
"Returns the nth item or empty list"
608
return list(islice(iterable, n, n+1))
610
def quantify(iterable, pred=bool):
611
"Count how many times the predicate is true"
612
return sum(imap(pred, iterable))
614
def padnone(iterable):
641
615
"""Returns the sequence elements and then returns None indefinitely.
643
617
Useful for emulating the behavior of the built-in map() function.
645
return chain(seq, repeat(None))
619
return chain(iterable, repeat(None))
621
def ncycles(iterable, n):
648
622
"Returns the sequence elements n times"
649
return chain.from_iterable(repeat(seq, n))
623
return chain.from_iterable(repeat(iterable, n))
651
625
def dotproduct(vec1, vec2):
652
626
return sum(imap(operator.mul, vec1, vec2))
671
645
return izip(a, b)
673
647
def grouper(n, iterable, fillvalue=None):
674
"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
648
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
675
649
args = [iter(iterable)] * n
676
kwds = dict(fillvalue=fillvalue)
677
return izip_longest(*args, **kwds)
650
return izip_longest(fillvalue=fillvalue, *args)
679
652
def roundrobin(*iterables):
680
"roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
653
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
681
654
# Recipe credited to George Sakkis
682
655
pending = len(iterables)
683
656
nexts = cycle(iter(it).next for it in iterables)
697
670
yield set(x for m, x in pairs if m&n)
699
672
def compress(data, selectors):
700
"compress('abcdef', [1,0,1,0,1,1]) --> a c e f"
701
for d, s in izip(data, selectors):
673
"compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
674
return (d for d, s in izip(data, selectors) if s)
676
def combinations_with_replacement(iterable, r):
677
"combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC"
678
pool = tuple(iterable)
681
yield tuple(pool[i] for i in indices)
683
for i in reversed(range(r)):
684
if indices[i] != n - 1:
688
indices[i:] = [indices[i] + 1] * (r - i)
689
yield tuple(pool[i] for i in indices)