~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Doc/library/itertools.rst

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
Likewise, the functional tools are designed to work well with the high-speed
36
36
functions provided by the :mod:`operator` module.
37
37
 
38
 
The module author welcomes suggestions for other basic building blocks to be
39
 
added to future versions of the module.
40
 
 
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".
45
42
 
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
 
 
50
43
 
51
44
.. seealso::
52
45
 
375
368
          except IndexError:
376
369
              pass
377
370
 
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``.
381
375
 
382
376
   .. versionadded:: 2.6
383
377
 
598
592
 
599
593
.. testcode::
600
594
 
601
 
   def take(n, seq):
602
 
       return list(islice(seq, n))
603
 
 
604
 
   def enumerate(iterable):
605
 
       return izip(count(), iterable)
606
 
 
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))
 
598
 
 
599
   def enumerate(iterable, start=0):
 
600
       return izip(count(start), iterable)
 
601
 
 
602
   def tabulate(function, start=0):
608
603
       "Return function(0), function(1), ..."
609
 
       return imap(function, count())
610
 
 
611
 
   def iteritems(mapping):
612
 
       return izip(mapping.iterkeys(), mapping.itervalues())
 
604
       return imap(function, count(start))
613
605
 
614
606
   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):
 
607
       "Returns the nth item or empty list"
 
608
       return list(islice(iterable, n, n+1))
 
609
 
 
610
   def quantify(iterable, pred=bool):
 
611
       "Count how many times the predicate is true"
 
612
       return sum(imap(pred, iterable))
 
613
 
 
614
   def padnone(iterable):
641
615
       """Returns the sequence elements and then returns None indefinitely.
642
616
 
643
617
       Useful for emulating the behavior of the built-in map() function.
644
618
       """
645
 
       return chain(seq, repeat(None))
 
619
       return chain(iterable, repeat(None))
646
620
 
647
 
   def ncycles(seq, n):
 
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))
650
624
 
651
625
   def dotproduct(vec1, vec2):
652
626
       return sum(imap(operator.mul, vec1, vec2))
671
645
       return izip(a, b)
672
646
 
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)
678
651
 
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)
698
671
 
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):
702
 
           if s:
703
 
               yield d
 
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)
 
675
 
 
676
   def combinations_with_replacement(iterable, r):
 
677
       "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC"
 
678
       pool = tuple(iterable)
 
679
       n = len(pool)
 
680
       indices = [0] * r
 
681
       yield tuple(pool[i] for i in indices)
 
682
       while 1:
 
683
           for i in reversed(range(r)):
 
684
               if indices[i] != n - 1:
 
685
                   break
 
686
           else:
 
687
               return
 
688
           indices[i:] = [indices[i] + 1] * (r - i)
 
689
           yield tuple(pool[i] for i in indices)