From 7e1f24a13d213d552f88cf6e17542f238d6be0cd Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Mon, 27 Jul 2009 04:01:20 -0400 Subject: [PATCH] xrefs --- advanced-iterators.html | 6 +++--- strings.html | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/advanced-iterators.html b/advanced-iterators.html index bdad90d..c6f9aad 100755 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -205,7 +205,7 @@ AssertionError: Only for very large values of 2 >>> tuple(ord(c) for c in unique_characters) (69, 68, 77, 79, 78, 83, 82, 89)
    -
  1. A generator expression is like an anonymous function that yields values. The expression itself looks like a list comprehension, but it’s wrapped in parentheses instead of square brackets. +
  2. A generator expression is like an anonymous function that yields values. The expression itself looks like a list comprehension, but it’s wrapped in parentheses instead of square brackets.
  3. The generator expression returns… an iterator.
  4. Calling next(gen) returns the next value from the iterator.
  5. If you like, you can iterate through all the possible values and return a tuple, list, or set, by passing the generator expression to tuple(), list(), or set(). In these cases, you don’t need an extra set of parentheses — just pass the “bare” expression ord(c) for c in unique_characters to the tuple() function, and Python figures out that it’s a generator expression. @@ -408,7 +408,7 @@ Wesley 'N': '5', 'S': '1', 'R': '6', 'Y': '7'}
    1. Given a list of letters and a list of digits (each represented here as 1-character strings), the zip function will create a pairing of letters and digits, in order. -
    2. Why is that cool? Because that data structure happens to be exactly the right structure to pass to the dict() 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 dictionary comprehension 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 characters and guess sequences. +
    3. Why is that cool? Because that data structure happens to be exactly the right structure to pass to the dict() 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 dictionary comprehension 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 characters and guess sequences.

    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

    1. Finds all the letters in the puzzle with the re.findall() function -
    2. Find all the unique letters in the puzzle with set comprehensions +
    3. Find all the unique letters in the puzzle with sets and the set() function
    4. Checks if there are more than 10 unique letters (meaning the puzzle is definitely unsolvable) with an assert statement
    5. Converts the letters to their ASCII equivalents with a generator object
    6. Calculates all the possible solutions with the itertools.permutations() function diff --git a/strings.html b/strings.html index 6d0fca9..98c02d1 100755 --- a/strings.html +++ b/strings.html @@ -264,7 +264,7 @@ experience of years.
      1. The split() 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. -
      2. Now we have a list of strings, each with a key, followed by an equals sign, followed by a value. We can use a list comprehension 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 'key=value=foo'.split('='), we would end up with a three-item list ['key', 'value', 'foo'].) +
      3. Now we have a list of strings, each with a key, followed by an equals sign, followed by a value. We can use a list comprehension 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 'key=value=foo'.split('='), we would end up with a three-item list ['key', 'value', 'foo'].)
      4. Finally, Python can turn that list-of-lists into a dictionary simply by passing it to the dict() function.