Repository navigation
Expand file tree
/
Copy pathItertools.html
More file actions
370 lines (232 loc) · 16.2 KB
/
Copy pathItertools.html
File metadata and controls
370 lines (232 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python’s “Itertools” — Python Like You Mean It 0.9.1 documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<link rel="index" title="Index"
href="../genindex.html"/>
<link rel="search" title="Search" href="../search.html"/>
<link rel="top" title="Python Like You Mean It 0.9.1 documentation" href="../index.html"/>
<link rel="up" title="Module 2: The Essentials of Python" href="../module_2.html"/>
<link rel="next" title="Basics of Functions" href="Functions.html"/>
<link rel="prev" title="Generators & Comprehension Expressions" href="Generators_and_Comprehensions.html"/>
<script src="../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../index.html" class="icon icon-home"> Python Like You Mean It
</a>
<div class="version">
0.9.1
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Table of Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_1.html">Module 1: Getting Started with Python</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../module_2.html">Module 2: The Essentials of Python</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="Basic_Objects.html">Basic Object Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="SequenceTypes.html">Sequence Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="Variables_and_Assignment.html">Variables & Assignment</a></li>
<li class="toctree-l2"><a class="reference internal" href="Introduction.html">Introducing Control Flow</a></li>
<li class="toctree-l2"><a class="reference internal" href="ConditionalStatements.html">Conditional Statements</a></li>
<li class="toctree-l2"><a class="reference internal" href="ForLoops.html">For-Loops and While-Loops</a></li>
<li class="toctree-l2"><a class="reference internal" href="Iterables.html">Iterables</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generators_and_Comprehensions.html">Generators & Comprehension Expressions</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Python’s “Itertools”</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#range"><code class="docutils literal"><span class="pre">range</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#enumerate"><code class="docutils literal"><span class="pre">enumerate</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#zip"><code class="docutils literal"><span class="pre">zip</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#itertools.chain"><code class="docutils literal"><span class="pre">itertools.chain</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="#itertools.combinations"><code class="docutils literal"><span class="pre">itertools.combinations</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="Functions.html">Basics of Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="Scope.html">Scope</a></li>
<li class="toctree-l2"><a class="reference internal" href="DataStructures.html">Data Structures (Part I): Introduction</a></li>
<li class="toctree-l2"><a class="reference internal" href="DataStructures_II_Dictionaries.html">Data Structures (Part II): Dictionaries</a></li>
<li class="toctree-l2"><a class="reference internal" href="DataStructures_III_Sets_and_More.html">Data Structures (Part III): Sets & the Collections Module</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../module_3.html">Module 3: The Essentials of NumPy</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Python Like You Mean It</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> »</li>
<li><a href="../module_2.html">Module 2: The Essentials of Python</a> »</li>
<li>Python’s “Itertools”</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/Module2_EssentialsOfPython/Itertools.ipynb.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<style>
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast,
.nboutput.nblast {
margin-bottom: 19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast + .nbinput {
margin-top: -19px;
}
/* nice headers on first paragraph of info/warning boxes */
.admonition .first {
margin: -12px;
padding: 6px 12px;
margin-bottom: 12px;
color: #fff;
line-height: 1;
display: block;
}
.admonition.warning .first {
background: #f0b37e;
}
.admonition.note .first {
background: #6ab0de;
}
.admonition > p:before {
margin-right: 4px; /* make room for the exclamation icon */
}
</style>
<div class="section" id="Python’s-“Itertools”">
<h1>Python’s “Itertools”<a class="headerlink" href="#Python’s-“Itertools”" title="Permalink to this headline">¶</a></h1>
<p>Python has an <a class="reference external" href="https://docs.python.org/3/library/itertools.html">itertools
module</a>, which
provides a core set of fast, memory-efficient tools for creating
iterators. The majority of these functions create generators. We will
briefly showcase a few itertools. It is hard to overstate the utility of
this module - it is strongly recommended that you take some time to see
what it has in store.</p>
<p>Actually, there are three functions that belong in itertools, but are so
useful that they are included in Python by default, and do not need to
be imported:</p>
<div class="section" id="range">
<h2><code class="docutils literal"><span class="pre">range</span></code><a class="headerlink" href="#range" title="Permalink to this headline">¶</a></h2>
<p>generate a sequence of integers in the specified “range”:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># will generate 0.. 1.. 2.. ... 8.. 9</span>
<span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="enumerate">
<h2><code class="docutils literal"><span class="pre">enumerate</span></code><a class="headerlink" href="#enumerate" title="Permalink to this headline">¶</a></h2>
<p>“enumerate” the items in an iterable:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># will generate (0, 'apple').. (1, 'banana').. (2, 'cat').. (3, 'dog')]</span>
<span class="nb">enumerate</span><span class="p">([</span><span class="s2">"apple"</span><span class="p">,</span> <span class="s2">"banana"</span><span class="p">,</span> <span class="s2">"cat"</span><span class="p">,</span> <span class="s2">"dog"</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="zip">
<h2><code class="docutils literal"><span class="pre">zip</span></code><a class="headerlink" href="#zip" title="Permalink to this headline">¶</a></h2>
<p>“zip” together the corresponding elements of several iterables into
tuples:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"Angie"</span><span class="p">,</span> <span class="s2">"Brian"</span><span class="p">,</span> <span class="s2">"Cassie"</span><span class="p">,</span> <span class="s2">"David"</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">exam_1_scores</span> <span class="o">=</span> <span class="p">[</span><span class="mi">90</span><span class="p">,</span> <span class="mi">82</span><span class="p">,</span> <span class="mi">79</span><span class="p">,</span> <span class="mi">87</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">exam_2_scores</span> <span class="o">=</span> <span class="p">[</span><span class="mi">95</span><span class="p">,</span> <span class="mi">84</span><span class="p">,</span> <span class="mi">72</span><span class="p">,</span> <span class="mi">91</span><span class="p">]</span>
<span class="go"># will generate ('Angie', 90, 95).. ('Brian', 82, 84).. ('Cassie', 79, 72).. ('David', 87, 91)]</span>
<span class="gp">>>> </span><span class="nb">zip</span><span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="n">exam_1_scores</span><span class="p">,</span> <span class="n">exam_2_scores</span><span class="p">)</span>
<span class="go"><zip at 0x20de1082608></span>
</pre></div>
</div>
<hr class="docutils" />
<p>The following are some of the many useful tools provided by the
<code class="docutils literal"><span class="pre">itertools</span></code> module:</p>
</div>
<div class="section" id="itertools.chain">
<h2><code class="docutils literal"><span class="pre">itertools.chain</span></code><a class="headerlink" href="#itertools.chain" title="Permalink to this headline">¶</a></h2>
<p>“chain” together multiple iterables, end-to-end:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">chain</span>
<span class="gp">>>> </span><span class="n">gen_1</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1"># 0.. 2.. 4</span>
<span class="gp">>>> </span><span class="n">gen_2</span> <span class="o">=</span> <span class="p">(</span><span class="n">i</span><span class="o">**</span><span class="mi">2</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span> <span class="c1"># 9.. 16.. 25</span>
<span class="gp">>>> </span><span class="n">iter_3</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"moo"</span><span class="p">,</span> <span class="s2">"cow"</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">iter_4</span> <span class="o">=</span> <span class="s2">"him"</span>
<span class="go"># will generate: 0.. 2.. 4.. 9.. 16.. 25.. 'moo'.. 'cow'.. 'h'.. 'i'.. 'm'</span>
<span class="gp">>>> </span><span class="n">chain</span><span class="p">(</span><span class="n">gen_1</span><span class="p">,</span> <span class="n">gen_2</span><span class="p">,</span> <span class="n">iter_3</span><span class="p">,</span> <span class="n">iter_4</span><span class="p">)</span>
<span class="go"><itertools.chain at 0x20de109ec18></span>
</pre></div>
</div>
</div>
<div class="section" id="itertools.combinations">
<h2><code class="docutils literal"><span class="pre">itertools.combinations</span></code><a class="headerlink" href="#itertools.combinations" title="Permalink to this headline">¶</a></h2>
<p>Generate all length-n “combinations” from an iterable:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">combinations</span>
<span class="go"># will generate: (0, 1, 2).. (0, 1, 3).. (0, 2, 3).. (1, 2, 3)</span>
<span class="gp">>>> </span><span class="n">combinations</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="mi">3</span><span class="p">)</span> <span class="c1"># generate all length-3 combinations from [0, 1, 2, 3]</span>
<span class="go"><itertools.combinations at 0x20de10a7728></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="Functions.html" class="btn btn-neutral float-right" title="Basics of Functions" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="Generators_and_Comprehensions.html" class="btn btn-neutral" title="Generators & Comprehension Expressions" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2018, Ryan Soklaski.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../',
VERSION:'0.9.1',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>