OLD | NEW |
1 :mod:`itertools` --- Functions creating iterators for efficient looping | 1 :mod:`itertools` --- Functions creating iterators for efficient looping |
2 ======================================================================= | 2 ======================================================================= |
3 | 3 |
4 .. module:: itertools | 4 .. module:: itertools |
5 :synopsis: Functions creating iterators for efficient looping. | 5 :synopsis: Functions creating iterators for efficient looping. |
6 .. moduleauthor:: Raymond Hettinger <python@rcn.com> | 6 .. moduleauthor:: Raymond Hettinger <python@rcn.com> |
7 .. sectionauthor:: Raymond Hettinger <python@rcn.com> | 7 .. sectionauthor:: Raymond Hettinger <python@rcn.com> |
8 | 8 |
9 | 9 |
10 .. testsetup:: | 10 .. testsetup:: |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 Itertool functions | 81 Itertool functions |
82 ------------------ | 82 ------------------ |
83 | 83 |
84 The following module functions all construct and return iterators. Some provide | 84 The following module functions all construct and return iterators. Some provide |
85 streams of infinite length, so they should only be accessed by functions or | 85 streams of infinite length, so they should only be accessed by functions or |
86 loops that truncate the stream. | 86 loops that truncate the stream. |
87 | 87 |
88 .. function:: accumulate(iterable[, func]) | 88 .. function:: accumulate(iterable[, func]) |
89 | 89 |
90 Make an iterator that returns accumulated sums. Elements may be any addable | 90 Make an iterator that returns accumulated sums. Elements may be any addable |
91 type including :class:`Decimal` or :class:`Fraction`. If the optional | 91 type including :class:`~decimal.Decimal` or :class:`~fractions.Fraction`. I
f the optional |
92 *func* argument is supplied, it should be a function of two arguments | 92 *func* argument is supplied, it should be a function of two arguments |
93 and it will be used instead of addition. | 93 and it will be used instead of addition. |
94 | 94 |
95 Equivalent to:: | 95 Equivalent to:: |
96 | 96 |
97 def accumulate(iterable, func=operator.add): | 97 def accumulate(iterable, func=operator.add): |
98 'Return running totals' | 98 'Return running totals' |
99 # accumulate([1,2,3,4,5]) --> 1 3 6 10 15 | 99 # accumulate([1,2,3,4,5]) --> 1 3 6 10 15 |
100 # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120 | 100 # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120 |
101 it = iter(iterable) | 101 it = iter(iterable) |
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 n = len(pool) | 805 n = len(pool) |
806 indices = sorted(random.randrange(n) for i in range(r)) | 806 indices = sorted(random.randrange(n) for i in range(r)) |
807 return tuple(pool[i] for i in indices) | 807 return tuple(pool[i] for i in indices) |
808 | 808 |
809 Note, many of the above recipes can be optimized by replacing global lookups | 809 Note, many of the above recipes can be optimized by replacing global lookups |
810 with local variables defined as default values. For example, the | 810 with local variables defined as default values. For example, the |
811 *dotproduct* recipe can be written as:: | 811 *dotproduct* recipe can be written as:: |
812 | 812 |
813 def dotproduct(vec1, vec2, sum=sum, map=map, mul=operator.mul): | 813 def dotproduct(vec1, vec2, sum=sum, map=map, mul=operator.mul): |
814 return sum(map(mul, vec1, vec2)) | 814 return sum(map(mul, vec1, vec2)) |
OLD | NEW |