From 856f79c30cfba5efb9aee5f8a0ef315b109034f5 Mon Sep 17 00:00:00 2001 From: Paul Resnick Date: Fri, 19 Oct 2018 15:07:36 -0400 Subject: [PATCH 1/4] improvements to AdvancedAccumulation --- _sources/AdvancedAccumulation/filter.rst | 8 +++---- _sources/AdvancedAccumulation/listcomp.rst | 8 +++---- _sources/AdvancedAccumulation/map.rst | 25 +++++++++++----------- _sources/AdvancedAccumulation/zip.rst | 6 +++--- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/_sources/AdvancedAccumulation/filter.rst b/_sources/AdvancedAccumulation/filter.rst index 02b4f02b..c62b5cc5 100644 --- a/_sources/AdvancedAccumulation/filter.rst +++ b/_sources/AdvancedAccumulation/filter.rst @@ -34,11 +34,11 @@ iterate with a for loop. .. activecode:: ac21_3_2 - def keep_odds(nums): - new_list = filter(lambda num: num % 2 == 1, nums) - return new_list + def keep_evens(nums): + new_seq = filter(lambda num: num % 2 == 0, nums) + return list(new_seq) - print(keep_odds([3, 4, 6, 7, 0, 1])) + print(keep_evens([3, 4, 6, 7, 0, 1])) **Check Your Understanding** diff --git a/_sources/AdvancedAccumulation/listcomp.rst b/_sources/AdvancedAccumulation/listcomp.rst index d7f3e347..1f0cbdd4 100644 --- a/_sources/AdvancedAccumulation/listcomp.rst +++ b/_sources/AdvancedAccumulation/listcomp.rst @@ -17,7 +17,7 @@ Python provides an alternative way to do ``map`` and ``filter`` operations, call Many programmers find them easier to understand and write. List comprehensions are concise ways to create lists from other lists. The general syntax is:: - [ for in if ] + [ for in if ] where the if clause is optional. For example, @@ -29,14 +29,14 @@ where the if clause is optional. For example, print(yourlist) -The expression is ``value * 2``. The item variable is ``value`` and the sequence is ``things``. This is an alternative way +The transformer expression is ``value * 2``. The item variable is ``value`` and the sequence is ``things``. This is an alternative way to perform a mapping operation. As with ``map``, each item in the sequence is transformed into an item in the new list. Instead of the iteration happening automatically, however, we have adopted the syntax of the for loop which may make it easier to understand. Just as in a regular for loop, the part of the statement ``for value in things`` says to execute some code once for each item in things. Each time that code is executed, ``value`` is bound to one item from ``things``. The code that is executed -each time is the expression at the beginning, ``value * 2``, rather than a block of code indented underneath the for +each time is the transformer expression, ``value * 2``, rather than a block of code indented underneath the for statement. The other difference from a regular for loop is that each time the expression is evaluated, the resulting value is appended to a list. That happens automatically, without the programmer explicitly initializing an empty list or appending each item. @@ -126,7 +126,7 @@ You can also combine ``map`` and ``filter`` operations by chaining them together :chatcodes: :practice: T - **3.** Write code to assign to the variable ``compri`` all the values of the key name in the dictionary ``tester``. Do this using list comprehension. + **3.** Write code to assign to the variable ``compri`` all the values of the key ``name`` in any of the sub-dictionaries in the dictionary ``tester``. Do this using a list comprehension. ~~~~ tester = {'info': [{"name": "Lauren", 'class standing': 'Junior', 'major': "Information Science"},{'name': 'Ayo', 'class standing': "Bachelor's", 'major': 'Information Science'}, {'name': 'Kathryn', 'class standing': 'Senior', 'major': 'Sociology'}, {'name': 'Nick', 'class standing': 'Junior', 'major': 'Computer Science'}, {'name': 'Gladys', 'class standing': 'Sophomore', 'major': 'History'}, {'name': 'Adam', 'major': 'Violin Performance', 'class standing': 'Senior'}]} diff --git a/_sources/AdvancedAccumulation/map.rst b/_sources/AdvancedAccumulation/map.rst index 411533e7..fe9ad296 100644 --- a/_sources/AdvancedAccumulation/map.rst +++ b/_sources/AdvancedAccumulation/map.rst @@ -59,6 +59,13 @@ makes it more clear what the overall structure of the computation is. ``map`` ta sequence. The function is the mapper that transforms items. It is automatically applied to each item in the sequence. You don't have to initialize an accumulator or iterate with a for loop at all. +.. note:: + + Technically, in a proper Python 3 interpreter, the ``map`` function produces an "iterator", which is like a list but + produces the items as they are needed. Most places in Python where you can use a list (e.g., in a for loop) you can + use an "iterator" as if it was actually a list. So you probably won't ever notice the difference. If you ever really + need a list, you can explicitly turn the output of map into a list: ``list(map(...))``. In the runestone environment, ``map`` actually returns a real list, but to make this code compatible with a full python environment, we always convert it to a list. + As we did when passing a function as a parameter to the ``sorted`` function, we can specify a function to pass to ``map`` either by referring to a function by name, or by providing a lambda expression. @@ -68,12 +75,12 @@ either by referring to a function by name, or by providing a lambda expression. return 3*value def tripleStuff(a_list): - new_list = map(triple, a_list) - return new_list + new_seq = map(triple, a_list) + return list(new_seq) def quadrupleStuff(a_list): - new_list = map(lambda value: 4*value, a_list) - return new_list + new_seq = map(lambda value: 4*value, a_list) + return list(new_seq) things = [2, 5, 9] things3 = tripleStuff(things) @@ -89,17 +96,11 @@ Of course, once we get used to using the ``map`` function, it's no longer necess things = [2, 5, 9] things4 = map((lambda value: 4*value), things) - print(things4) + print(list(things4)) # or all on one line - print(map((lambda value: 5*value), [1, 2, 3])) - -.. note:: + print(list(map((lambda value: 5*value), [1, 2, 3]))) - Technically, in a proper Python 3 interpreter, the ``map`` function produces an "iterator", which is like a list but - produces the items as they are needed. Most places in Python where you can use a list (e.g., in a for loop) you can - use an "iterator" as if it was actually a list. So you probably won't ever notice the difference. If you ever really - need a list, you can explicitly turn the output of map into a list: ``list(map(...))``. **Check Your Understanding** diff --git a/_sources/AdvancedAccumulation/zip.rst b/_sources/AdvancedAccumulation/zip.rst index cb25401f..a2ce6dcb 100644 --- a/_sources/AdvancedAccumulation/zip.rst +++ b/_sources/AdvancedAccumulation/zip.rst @@ -46,7 +46,7 @@ so on. L1 = [3, 4, 5] L2 = [1, 2, 3] - L4 = zip(L1, L2) + L4 = list(zip(L1, L2)) print(L4) Here's what happens when you loop through the tuples. @@ -56,7 +56,7 @@ Here's what happens when you loop through the tuples. L1 = [3, 4, 5] L2 = [1, 2, 3] L3 = [] - L4 = zip(L1, L2) + L4 = list(zip(L1, L2)) for (x1, x2) in L4: L3.append(x1+x2) @@ -69,7 +69,7 @@ Or, simplifying and using a list comprehension: L1 = [3, 4, 5] L2 = [1, 2, 3] - L3 = [x1 + x2 for (x1, x2) in zip(L1, L2)] + L3 = [x1 + x2 for (x1, x2) in list(zip(L1, L2))] print(L3) Or, using ``map`` and not unpacking the tuple (our online environment has trouble with unpacking the tuple in a lambda expression): From 692cea43920f17cd5e155a6a2c92b07faaa834ec Mon Sep 17 00:00:00 2001 From: Paul Resnick Date: Wed, 2 Jan 2019 18:30:26 -0500 Subject: [PATCH 2/4] fix typo --- _sources/Debugging/KnowyourerrorMessages.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_sources/Debugging/KnowyourerrorMessages.rst b/_sources/Debugging/KnowyourerrorMessages.rst index f84f9611..298f5b64 100644 --- a/_sources/Debugging/KnowyourerrorMessages.rst +++ b/_sources/Debugging/KnowyourerrorMessages.rst @@ -329,7 +329,7 @@ See if you can get this program to run successfully: .. activecode:: ac4_7_5 str_time = input("What time is it now?") - str_wait_time = input("What is the number of nours to wait?") + str_wait_time = input("What is the number of hours to wait?") time = int(str_time) wai_time = int(str_wait_time) From c2169deac7e194daf94ffb28cc50204a1b43048e Mon Sep 17 00:00:00 2001 From: Paul Resnick Date: Mon, 17 Dec 2018 17:04:34 -0500 Subject: [PATCH 3/4] remove punctuation from tests --- _sources/Dictionaries/ChapterAssessment.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_sources/Dictionaries/ChapterAssessment.rst b/_sources/Dictionaries/ChapterAssessment.rst index df848d73..eed17ae0 100644 --- a/_sources/Dictionaries/ChapterAssessment.rst +++ b/_sources/Dictionaries/ChapterAssessment.rst @@ -298,7 +298,7 @@ Assessment - Dictionary Accumulation class myTests(TestCaseGui): def testOne(self): - self.assertEqual(sorted(wrd_d.items()), sorted([('Singing', 1), ('in', 2), ('the', 2), ('rain', 2), ('and', 1), ('playing', 1), ('are', 1), ('two', 1), ('entirely', 1), ('different', 1), ('situations,', 1), ('but', 1), ('both', 1), ('can', 1), ('be', 1), ('good.', 1)]), "Testing that wrd_d has been created correctly.") + self.assertEqual(sorted(wrd_d.items()), sorted([('Singing', 1), ('in', 2), ('the', 2), ('rain', 2), ('and', 1), ('playing', 1), ('are', 1), ('two', 1), ('entirely', 1), ('different', 1), ('situations', 1), ('but', 1), ('both', 1), ('can', 1), ('be', 1), ('good', 1)]), "Testing that wrd_d has been created correctly.") myTests().main() From fd1e293bc7f70f10db301bb991d6e3d5c37da0bc Mon Sep 17 00:00:00 2001 From: Paul Resnick Date: Thu, 25 Oct 2018 13:22:20 -0400 Subject: [PATCH 4/4] add missing import json --- _sources/NestedData/jsonlib.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/_sources/NestedData/jsonlib.rst b/_sources/NestedData/jsonlib.rst index f6a3a5c4..8115dc2a 100644 --- a/_sources/NestedData/jsonlib.rst +++ b/_sources/NestedData/jsonlib.rst @@ -40,6 +40,7 @@ The other function we will use is ``dumps``. It does the inverse of ``loads``. I .. activecode:: ac17_3_2 :language: python + import json def pretty(obj): return json.dumps(obj, sort_keys=True, indent=2)