Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="itertools-example.html" /> | |
13 | <link rel="parent" href="module-itertools.html" /> | |
14 | <link rel="next" href="module-ConfigParser.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>5.16.3 Recipes </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="5.16.2 Examples" | |
24 | href="itertools-example.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="5.16 itertools " | |
27 | href="module-itertools.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="5.17 ConfigParser " | |
30 | href="module-ConfigParser.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="itertools-example.html">5.16.2 Examples</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-itertools.html">5.16 itertools </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-ConfigParser.html">5.17 ConfigParser </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION0071630000000000000000"></A><A NAME="itertools-recipes"></A> | |
55 | <BR> | |
56 | 5.16.3 Recipes | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | This section shows recipes for creating an extended toolset using the | |
61 | existing itertools as building blocks. | |
62 | ||
63 | <P> | |
64 | The extended tools offer the same high performance as the underlying | |
65 | toolset. The superior memory performance is kept by processing elements one | |
66 | at a time rather than bringing the whole iterable into memory all at once. | |
67 | Code volume is kept small by linking the tools together in a functional style | |
68 | which helps eliminate temporary variables. High speed is retained by | |
69 | preferring ``vectorized'' building blocks over the use of for-loops and | |
70 | generators which incur interpreter overhead. | |
71 | ||
72 | <P> | |
73 | <div class="verbatim"><pre> | |
74 | def take(n, seq): | |
75 | return list(islice(seq, n)) | |
76 | ||
77 | def enumerate(iterable): | |
78 | return izip(count(), iterable) | |
79 | ||
80 | def tabulate(function): | |
81 | "Return function(0), function(1), ..." | |
82 | return imap(function, count()) | |
83 | ||
84 | def iteritems(mapping): | |
85 | return izip(mapping.iterkeys(), mapping.itervalues()) | |
86 | ||
87 | def nth(iterable, n): | |
88 | "Returns the nth item" | |
89 | return list(islice(iterable, n, n+1)) | |
90 | ||
91 | def all(seq, pred=bool): | |
92 | "Returns True if pred(x) is True for every element in the iterable" | |
93 | for elem in ifilterfalse(pred, seq): | |
94 | return False | |
95 | return True | |
96 | ||
97 | def any(seq, pred=bool): | |
98 | "Returns True if pred(x) is True for at least one element in the iterable" | |
99 | for elem in ifilter(pred, seq): | |
100 | return True | |
101 | return False | |
102 | ||
103 | def no(seq, pred=bool): | |
104 | "Returns True if pred(x) is False for every element in the iterable" | |
105 | for elem in ifilter(pred, seq): | |
106 | return False | |
107 | return True | |
108 | ||
109 | def quantify(seq, pred=bool): | |
110 | "Count how many times the predicate is True in the sequence" | |
111 | return sum(imap(pred, seq)) | |
112 | ||
113 | def padnone(seq): | |
114 | """Returns the sequence elements and then returns None indefinitely. | |
115 | ||
116 | Useful for emulating the behavior of the built-in map() function. | |
117 | """ | |
118 | return chain(seq, repeat(None)) | |
119 | ||
120 | def ncycles(seq, n): | |
121 | "Returns the sequence elements n times" | |
122 | return chain(*repeat(seq, n)) | |
123 | ||
124 | def dotproduct(vec1, vec2): | |
125 | return sum(imap(operator.mul, vec1, vec2)) | |
126 | ||
127 | def flatten(listOfLists): | |
128 | return list(chain(*listOfLists)) | |
129 | ||
130 | def repeatfunc(func, times=None, *args): | |
131 | """Repeat calls to func with specified arguments. | |
132 | ||
133 | Example: repeatfunc(random.random) | |
134 | """ | |
135 | if times is None: | |
136 | return starmap(func, repeat(args)) | |
137 | else: | |
138 | return starmap(func, repeat(args, times)) | |
139 | ||
140 | def pairwise(iterable): | |
141 | "s -> (s0,s1), (s1,s2), (s2, s3), ..." | |
142 | a, b = tee(iterable) | |
143 | try: | |
144 | b.next() | |
145 | except StopIteration: | |
146 | pass | |
147 | return izip(a, b) | |
148 | </pre></div> | |
149 | ||
150 | <DIV CLASS="navigation"> | |
151 | <div class='online-navigation'> | |
152 | <p></p><hr /> | |
153 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
154 | <tr> | |
155 | <td class='online-navigation'><a rel="prev" title="5.16.2 Examples" | |
156 | href="itertools-example.html"><img src='../icons/previous.png' | |
157 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
158 | <td class='online-navigation'><a rel="parent" title="5.16 itertools " | |
159 | href="module-itertools.html"><img src='../icons/up.png' | |
160 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
161 | <td class='online-navigation'><a rel="next" title="5.17 ConfigParser " | |
162 | href="module-ConfigParser.html"><img src='../icons/next.png' | |
163 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
164 | <td align="center" width="100%">Python Library Reference</td> | |
165 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
166 | href="contents.html"><img src='../icons/contents.png' | |
167 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
168 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
169 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
170 | <td class='online-navigation'><a rel="index" title="Index" | |
171 | href="genindex.html"><img src='../icons/index.png' | |
172 | border='0' height='32' alt='Index' width='32' /></A></td> | |
173 | </tr></table> | |
174 | <div class='online-navigation'> | |
175 | <b class="navlabel">Previous:</b> | |
176 | <a class="sectref" rel="prev" href="itertools-example.html">5.16.2 Examples</A> | |
177 | <b class="navlabel">Up:</b> | |
178 | <a class="sectref" rel="parent" href="module-itertools.html">5.16 itertools </A> | |
179 | <b class="navlabel">Next:</b> | |
180 | <a class="sectref" rel="next" href="module-ConfigParser.html">5.17 ConfigParser </A> | |
181 | </div> | |
182 | </div> | |
183 | <hr /> | |
184 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
185 | </DIV> | |
186 | <!--End of Navigation Panel--> | |
187 | <ADDRESS> | |
188 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
189 | </ADDRESS> | |
190 | </BODY> | |
191 | </HTML> |