diff --git a/advanced-iterators.html b/advanced-iterators.html index d56e10b..2476728 100644 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -12,7 +12,7 @@ body{counter-reset:h1 6}

You are here: Home Dive Into Python 3

Advanced Iterators

-

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.
Leon Lederman +

Life is playfulness. We need to play so that we can rediscover the magical around us.
— Flora Colao

 

Diving In

@@ -123,35 +123,44 @@ AssertionError

Calculating Permutations… The Lazy Way!

-

FIXME what are permutations? +

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, Wikipedia is your friend.) + +

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.

->>> import itertools
->>> perms = itertools.permutations([1, 2, 3], 2)
->>> next(perms)
+>>> import itertools                              
+>>> perms = itertools.permutations([1, 2, 3], 2)  
+>>> next(perms)                                   
 (1, 2)
 >>> next(perms)
 (1, 3)
 >>> next(perms)
-(2, 1)
+(2, 1)                                            
 >>> next(perms)
 (2, 3)
 >>> next(perms)
 (3, 1)
 >>> next(perms)
 (3, 2)
->>> next(perms)
+>>> next(perms)                                   
 Traceback (most recent call last):
   File "<stdin>", line 1, in 
 StopIteration
+
    +
  1. The itertools module has all kinds of fun stuff in it, including a permutations() function that does all the hard work of finding permutations. +
  2. The permutations() 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 foor loop or any old place that iterates. Here I'll step through the iterator manually to show all the values. +
  3. The first permutation of [1, 2, 3] taken 2 at a time is (1, 2). +
  4. Note that permutations are ordered: (2, 1) is different than (1, 2). +
  5. That's it! Those are all the permutations of [1, 2, 3] taken 2 at a time. Pairs like (1, 1) and (2, 2) never show up, because they contain repeats so they aren't valid permutations. When there are no more permutations, the iterator raises a StopIteration exception. +
-

FIXME +

The permutations() function doesn't have to take a list. It can take any sequence — even a string.

 >>> import itertools
->>> perms = itertools.permutations('ABC', 3)
+>>> perms = itertools.permutations('ABC', 3)  
 >>> next(perms)
-('A', 'B', 'C')
+('A', 'B', 'C')                               
 >>> next(perms)
 ('A', 'C', 'B')
 >>> next(perms)
@@ -166,14 +175,17 @@ StopIteration
Traceback (most recent call last): File "<stdin>", line 1, in StopIteration ->>> list(itertools.permutations('ABC', 3)) +>>> list(itertools.permutations('ABC', 3)) [('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')] +
    +
  1. A string is just a sequence of characters. For the purposes of finding permutations, the string 'ABC' is equivalent to the list ['A', 'B', 'C']. +
  2. The first permutation of the 3 items ['A', 'B', 'C'], taken 3 at a time, is ('A', 'B', 'C'). There are five other permutations — the same three characters in every conceivable order. +
  3. Since the permutations() function always returns an iterator, an easy way to debug permutations is to pass that iterator to the built-in list() function to see all the permutations immediately. +
-

FIXME - -

Other Fun Stuff In The itertools Module

+

Other Fun Stuff in the itertools Module

 >>> import itertools
 >>> list(itertools.product('ABC', '123'))
diff --git a/dip3.css b/dip3.css
index 7cae60f..1272460 100644
--- a/dip3.css
+++ b/dip3.css
@@ -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}
diff --git a/index.html b/index.html
index 005ffba..a9e3c1d 100644
--- a/index.html
+++ b/index.html
@@ -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}