Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates, mostly to advanced accumulation chapter #164

Merged
merged 4 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions _sources/AdvancedAccumulation/filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
8 changes: 4 additions & 4 deletions _sources/AdvancedAccumulation/listcomp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

[<expression> for <item> in <sequence> if <condition>]
[<transformer_expression> for <loop_var> in <sequence> if <filtration_expression>]

where the if clause is optional. For example,

Expand All @@ -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.
Expand Down Expand Up @@ -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'}]}
Expand Down
25 changes: 13 additions & 12 deletions _sources/AdvancedAccumulation/map.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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)
Expand All @@ -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**

Expand Down
6 changes: 3 additions & 3 deletions _sources/AdvancedAccumulation/zip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion _sources/Debugging/KnowyourerrorMessages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion _sources/Dictionaries/ChapterAssessment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions _sources/NestedData/jsonlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down