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="next" href="itertools-recipes.html" /> | |
13 | <link rel="prev" href="itertools-functions.html" /> | |
14 | <link rel="parent" href="module-itertools.html" /> | |
15 | <link rel="next" href="itertools-recipes.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5.16.2 Examples </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="5.16.1 Itertool functions" | |
25 | href="itertools-functions.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="5.16 itertools " | |
28 | href="module-itertools.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="5.16.3 Recipes" | |
31 | href="itertools-recipes.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="itertools-functions.html">5.16.1 Itertool functions</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-itertools.html">5.16 itertools </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="itertools-recipes.html">5.16.3 Recipes</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION0071620000000000000000"></A><A NAME="itertools-example"></A> | |
56 | <BR> | |
57 | 5.16.2 Examples | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | The following examples show common uses for each tool and | |
62 | demonstrate ways they can be combined. | |
63 | ||
64 | <P> | |
65 | <div class="verbatim"><pre> | |
66 | >>> amounts = [120.15, 764.05, 823.14] | |
67 | >>> for checknum, amount in izip(count(1200), amounts): | |
68 | ... print 'Check %d is for $%.2f' % (checknum, amount) | |
69 | ... | |
70 | Check 1200 is for $120.15 | |
71 | Check 1201 is for $764.05 | |
72 | Check 1202 is for $823.14 | |
73 | ||
74 | >>> import operator | |
75 | >>> for cube in imap(operator.pow, xrange(1,5), repeat(3)): | |
76 | ... print cube | |
77 | ... | |
78 | 1 | |
79 | 8 | |
80 | 27 | |
81 | 64 | |
82 | ||
83 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', | |
84 | '', 'martin', '', 'walter', '', 'mark'] | |
85 | >>> for name in islice(reportlines, 3, None, 2): | |
86 | ... print name.title() | |
87 | ... | |
88 | Alex | |
89 | Laura | |
90 | Martin | |
91 | Walter | |
92 | Mark | |
93 | ||
94 | # Show a dictionary sorted and grouped by value | |
95 | >>> from operator import itemgetter | |
96 | >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) | |
97 | >>> di = sorted(d.iteritems(), key=itemgetter(1)) | |
98 | >>> for k, g in groupby(di, key=itemgetter(1)): | |
99 | ... print k, map(itemgetter(0), g) | |
100 | ... | |
101 | 1 ['a', 'c', 'e'] | |
102 | 2 ['b', 'd', 'f'] | |
103 | 3 ['g'] | |
104 | ||
105 | # Find runs of consecutive numbers using groupby. The key to the solution | |
106 | # is differencing with a range so that consecutive numbers all appear in | |
107 | # same group. | |
108 | >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] | |
109 | >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x): | |
110 | ... print map(operator.itemgetter(1), g) | |
111 | ... | |
112 | [1] | |
113 | [4, 5, 6] | |
114 | [10] | |
115 | [15, 16, 17, 18] | |
116 | [22] | |
117 | [25, 26, 27, 28] | |
118 | </pre></div> | |
119 | ||
120 | <P> | |
121 | ||
122 | <DIV CLASS="navigation"> | |
123 | <div class='online-navigation'> | |
124 | <p></p><hr /> | |
125 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
126 | <tr> | |
127 | <td class='online-navigation'><a rel="prev" title="5.16.1 Itertool functions" | |
128 | href="itertools-functions.html"><img src='../icons/previous.png' | |
129 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
130 | <td class='online-navigation'><a rel="parent" title="5.16 itertools " | |
131 | href="module-itertools.html"><img src='../icons/up.png' | |
132 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
133 | <td class='online-navigation'><a rel="next" title="5.16.3 Recipes" | |
134 | href="itertools-recipes.html"><img src='../icons/next.png' | |
135 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
136 | <td align="center" width="100%">Python Library Reference</td> | |
137 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
138 | href="contents.html"><img src='../icons/contents.png' | |
139 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
140 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
141 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
142 | <td class='online-navigation'><a rel="index" title="Index" | |
143 | href="genindex.html"><img src='../icons/index.png' | |
144 | border='0' height='32' alt='Index' width='32' /></A></td> | |
145 | </tr></table> | |
146 | <div class='online-navigation'> | |
147 | <b class="navlabel">Previous:</b> | |
148 | <a class="sectref" rel="prev" href="itertools-functions.html">5.16.1 Itertool functions</A> | |
149 | <b class="navlabel">Up:</b> | |
150 | <a class="sectref" rel="parent" href="module-itertools.html">5.16 itertools </A> | |
151 | <b class="navlabel">Next:</b> | |
152 | <a class="sectref" rel="next" href="itertools-recipes.html">5.16.3 Recipes</A> | |
153 | </div> | |
154 | </div> | |
155 | <hr /> | |
156 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
157 | </DIV> | |
158 | <!--End of Navigation Panel--> | |
159 | <ADDRESS> | |
160 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
161 | </ADDRESS> | |
162 | </BODY> | |
163 | </HTML> |