mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
xrefs
This commit is contained in:
@@ -205,7 +205,7 @@ AssertionError: Only for very large values of 2</samp></pre>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>tuple(ord(c) for c in unique_characters)</kbd> <span class=u>④</span></a>
|
||||
<samp class=pp>(69, 68, 77, 79, 78, 83, 82, 89)</samp></pre>
|
||||
<ol>
|
||||
<li>A generator expression is like an anonymous function that yields values. The expression itself looks like a <a href=comprehensions.htmllist-comprehensions>list comprehension</a>, but it’s wrapped in parentheses instead of square brackets.
|
||||
<li>A generator expression is like an anonymous function that yields values. The expression itself looks like a <a href=comprehensions.html#listcomprehension>list comprehension</a>, but it’s wrapped in parentheses instead of square brackets.
|
||||
<li>The generator expression returns… an iterator.
|
||||
<li>Calling <code>next(<var>gen</var>)</code> returns the next value from the iterator.
|
||||
<li>If you like, you can iterate through all the possible values and return a tuple, list, or set, by passing the generator expression to <code>tuple()</code>, <code>list()</code>, or <code>set()</code>. In these cases, you don’t need an extra set of parentheses — just pass the “bare” expression <code>ord(c) for c in unique_characters</code> to the <code>tuple()</code> function, and Python figures out that it’s a generator expression.
|
||||
@@ -408,7 +408,7 @@ Wesley</samp></pre>
|
||||
'N': '5', 'S': '1', 'R': '6', 'Y': '7'}</samp></pre>
|
||||
<ol>
|
||||
<li>Given a list of letters and a list of digits (each represented here as 1-character strings), the <code>zip</code> function will create a pairing of letters and digits, in order.
|
||||
<li>Why is that cool? Because that data structure happens to be exactly the right structure to pass to the <code>dict()</code> function to create a dictionary that uses letters as keys and their associated digits as values. (This isn’t the only way to do it, of course. You could use a <a href=comprehensions.html#dictionary-comprehensions>dictionary comprehension</a> to create the dictionary directly.) Although the printed representation of the dictionary lists the pairs in a different order (dictionaries have no “order” per se), you can see that each letter is associated with the digit, based on the ordering of the original <var>characters</var> and <var>guess</var> sequences.
|
||||
<li>Why is that cool? Because that data structure happens to be exactly the right structure to pass to the <code>dict()</code> function to create a dictionary that uses letters as keys and their associated digits as values. (This isn’t the only way to do it, of course. You could use a <a href=comprehensions.html#dictionarycomprehension>dictionary comprehension</a> to create the dictionary directly.) Although the printed representation of the dictionary lists the pairs in a different order (dictionaries have no “order” per se), you can see that each letter is associated with the digit, based on the ordering of the original <var>characters</var> and <var>guess</var> sequences.
|
||||
</ol>
|
||||
|
||||
<p id=guess>The alphametics solver uses this technique to create a dictionary that maps letters in the puzzle to digits in the solution, for each possible solution.
|
||||
@@ -610,7 +610,7 @@ NameError: name '__import__' is not defined</samp></pre>
|
||||
|
||||
<ol>
|
||||
<li><a href=#re-findall>Finds all the letters in the puzzle</a> with the <code>re.findall()</code> function
|
||||
<li><a href=#unique-items>Find all the <em>unique</em> letters in the puzzle</a> with set comprehensions
|
||||
<li><a href=#unique-items>Find all the <em>unique</em> letters in the puzzle</a> with sets and the <code>set()</code> function
|
||||
<li><a href=#assert>Checks if there are more than 10 unique letters</a> (meaning the puzzle is definitely unsolvable) with an <code>assert</code> statement
|
||||
<li><a href=#generator-objects>Converts the letters to their ASCII equivalents</a> with a generator object
|
||||
<li><a href=#permutations>Calculates all the possible solutions</a> with the <code>itertools.permutations()</code> function
|
||||
|
||||
+1
-1
@@ -264,7 +264,7 @@ experience of years.</samp>
|
||||
|
||||
<ol>
|
||||
<li>The <code><dfn>split</dfn>()</code> string method takes one argument, a delimiter, and split a string into a list of strings based on the delimiter. Here, the delimiter is an ampersand character, but it could be anything.
|
||||
<li>Now we have a list of strings, each with a key, followed by an equals sign, followed by a value. We can use a <a href=comprehensions.html#list-comprehensions>list comprehension</a> to iterate over the entire list and split each string into two strings based on the first equals sign. (In theory, a value could contain an equals sign too. If we just used <code>'key=value=foo'.split('=')</code>, we would end up with a three-item list <code>['key', 'value', 'foo']</code>.)
|
||||
<li>Now we have a list of strings, each with a key, followed by an equals sign, followed by a value. We can use a <a href=comprehensions.html#listcomprehension>list comprehension</a> to iterate over the entire list and split each string into two strings based on the first equals sign. (In theory, a value could contain an equals sign too. If we just used <code>'key=value=foo'.split('=')</code>, we would end up with a three-item list <code>['key', 'value', 'foo']</code>.)
|
||||
<li>Finally, Python can turn that list-of-lists into a dictionary simply by passing it to the <code>dict()</code> function.
|
||||
</ol>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user