mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
first section of advanced-iterators chapter
This commit is contained in:
+26
-14
@@ -12,7 +12,7 @@ body{counter-reset:h1 6}
|
||||
<p>You are here: <a href=index.html>Home</a> <span>‣</span> <a href=table-of-contents.html#advanced-iterators>Dive Into Python 3</a> <span>‣</span>
|
||||
<h1>Advanced Iterators</h1>
|
||||
<blockquote class=q>
|
||||
<p><span>❝</span> My ambition is to live to see all of physics reduced to a formula so elegant and simple that it will fit easily on the front of a t-shirt. <span>❞</span><br>— <a href="http://faculty.washington.edu/lynnhank/Lederman2.pdf">Leon Lederman</a>
|
||||
<p><span>❝</span> Life is playfulness. We need to play so that we can rediscover the magical around us. <span>❞</span><br>— Flora Colao
|
||||
</blockquote>
|
||||
<p id=toc>
|
||||
<h2 id=divingin>Diving In</h2>
|
||||
@@ -123,35 +123,44 @@ AssertionError</samp></pre>
|
||||
|
||||
<h2 id=permutations>Calculating Permutations… The Lazy Way!</h2>
|
||||
|
||||
<p>FIXME what are permutations?
|
||||
<p>First of all, what the heck are permutations? Permutations are a mathematical concept. (There are actually several definitions, depending on what kind of math you're doing. Here I'm talking about combinatorics, but if that doesn't mean anything to you, don't worry about it. As always, <a href="http://en.wikipedia.org/wiki/Permutation">Wikipedia is your friend</a>.)
|
||||
|
||||
<p>The idea is that you take a list of things (could be numbers, could be letters, could be dancing bears) and find all the possible ways to split them up into smaller lists. All the smaller lists have the same size, which can be as small as 1 and as large as the total number of items. Oh, and nothing can be repeated. Mathematicians say things like "let's find the permutations of 3 different items taken 2 at a time," which means you have a sequence of 3 items and you want to find all the possible ordered pairs.
|
||||
|
||||
<pre class=screen>
|
||||
<samp class=p>>>> </samp><kbd>import itertools</kbd>
|
||||
<samp class=p>>>> </samp><kbd>perms = itertools.permutations([1, 2, 3], 2)</kbd>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>import itertools</kbd> <span>①</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd>perms = itertools.permutations([1, 2, 3], 2)</kbd> <span>②</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd>next(perms)</kbd> <span>③</span></a>
|
||||
<samp>(1, 2)</samp>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<samp>(1, 3)</samp>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<samp>(2, 1)</samp>
|
||||
<a><samp>(2, 1)</samp> <span>④</span></a>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<samp>(2, 3)</samp>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<samp>(3, 1)</samp>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<samp>(3, 2)</samp>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>next(perms)</kbd> <span>⑤</span></a>
|
||||
<samp class=traceback>Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
StopIteration</samp></pre>
|
||||
<ol>
|
||||
<li>The <code>itertools</code> module has all kinds of fun stuff in it, including a <ocde>permutations()</code> function that does all the hard work of finding permutations.
|
||||
<li>The <code>permutations()</code> function takes a sequence (here a list of three integers) and a number, which is the number of items you want in each smaller group. The function returns an iterator, which you can use in a <code>foor</code> loop or any old place that iterates. Here I'll step through the iterator manually to show all the values.
|
||||
<li>The first permutation of <code>[1, 2, 3]</code> taken 2 at a time is <code>(1, 2)</code>.
|
||||
<li>Note that permutations are ordered: <code>(2, 1)</code> is different than <code>(1, 2)</code>.
|
||||
<li>That's it! Those are all the permutations of <code>[1, 2, 3]</code> taken 2 at a time. Pairs like <code>(1, 1)</code> and <code>(2, 2)</code> never show up, because they contain repeats so they aren't valid permutations. When there are no more permutations, the iterator raises a <code>StopIteration</code> exception.
|
||||
</ol>
|
||||
|
||||
<p>FIXME
|
||||
<p>The <code>permutations()</code> function doesn't have to take a list. It can take any sequence — even a string.
|
||||
|
||||
<pre class=screen>
|
||||
<samp class=p>>>> </samp><kbd>import itertools</kbd>
|
||||
<samp class=p>>>> </samp><kbd>perms = itertools.permutations('ABC', 3)</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>perms = itertools.permutations('ABC', 3)</kbd> <span>①</span></a>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<samp>('A', 'B', 'C')</samp>
|
||||
<a><samp>('A', 'B', 'C')</samp> <span>②</span></a>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
<samp>('A', 'C', 'B')</samp>
|
||||
<samp class=p>>>> </samp><kbd>next(perms)</kbd>
|
||||
@@ -166,14 +175,17 @@ StopIteration</samp></pre>
|
||||
<samp class=traceback>Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
StopIteration</samp>
|
||||
<samp class=p>>>> </samp><kbd>list(itertools.permutations('ABC', 3))</kbd>
|
||||
<a><samp class=p>>>> </samp><kbd>list(itertools.permutations('ABC', 3))</kbd> <span>③</span></a>
|
||||
<samp>[('A', 'B', 'C'), ('A', 'C', 'B'),
|
||||
('B', 'A', 'C'), ('B', 'C', 'A'),
|
||||
('C', 'A', 'B'), ('C', 'B', 'A')]</samp></pre>
|
||||
<ol>
|
||||
<li>A string is just a sequence of characters. For the purposes of finding permutations, the string <code>'ABC'</code> is equivalent to the list <code>['A', 'B', 'C']</code>.
|
||||
<li>The first permutation of the 3 items <code>['A', 'B', 'C']</code>, taken 3 at a time, is <code>('A', 'B', 'C')</code>. There are five other permutations — the same three characters in every conceivable order.
|
||||
<li>Since the <code>permutations()</code> function always returns an iterator, an easy way to debug permutations is to pass that iterator to the built-in <code>list()</code> function to see all the permutations immediately.
|
||||
</ol>
|
||||
|
||||
<p>FIXME
|
||||
|
||||
<h3 id=more-itertools>Other Fun Stuff In The <code>itertools</code> Module</h3>
|
||||
<h3 id=more-itertools>Other Fun Stuff in the <code>itertools</code> Module</h3>
|
||||
<pre class=screen>
|
||||
<samp class=p>>>> </samp><kbd>import itertools</kbd>
|
||||
<samp class=p>>>> </samp><kbd>list(itertools.product('ABC', '123'))</kbd>
|
||||
|
||||
@@ -67,8 +67,9 @@ p,ul,ol{margin:1.75em 0;font-size:medium}
|
||||
/* basics */
|
||||
html{background:#fff;color:#222}
|
||||
body{margin:1.75em 28px}
|
||||
form div{float:right}
|
||||
.c{text-align:center;margin:2.154em 0}
|
||||
form div{float:right}
|
||||
.todo{color:#ddd}
|
||||
|
||||
/* links */
|
||||
a{text-decoration:none;border-bottom:1px dotted}
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
h1:before{content:""}
|
||||
#a{list-style:none;margin:0 0 0 -1.7em}
|
||||
#a:before{content:"A. \00a0 \00a0"}
|
||||
.todo{color:#ddd}
|
||||
</style>
|
||||
<link rel=stylesheet type=text/css media='only screen and (max-device-width: 480px)' href=mobile.css>
|
||||
</head>
|
||||
|
||||
Reference in New Issue
Block a user