Written by Juliette Woodrow, Brahm Capoor, Anna Mistele, John Dalloul, and Elyse Cornwall
map Around the GlobeYou and your freshman year roommate grew up in different countries. You often have trouble trying to explain the current temperature outside as one of you is familiar with degrees Celsius and the other is used to using degrees Fahrenheit. Fortunately, you're taking 106A and can help!
Given a list of temperatures in degrees Farenheit like so:
temps_f = [45.7, 55.3, 62.1, 75.4, 32.0, 0.0, 100.0]
We're going to use map to produce a list of those same temperatures,
converted to degrees Celsius. First we'll use map with a helper function, and then we'll use
map with a
lambda function. To convert Farenheit to Celsius, you first subtract 32 and then multiply by
5⁄9.
Write a helper function def to_celcius(temp_f) that takes in a float representing a temperature
in
Fahrenheit temp_f, and returns that temperature in Celcius. Once you've written your
helper function, write a map statement to apply this helper function to every item in our list
temps_f and create a new list temps_c that contains the temperatures in Celcius.
Now, write a map statement using a lambda function and create your new temps_c
list this way. Don't use your helper function this time - write all the code inside the map!
You can write your code in using_map.py in the PyCharm project, and test it by running
python3 using_map.py. Hint: If you're running
this and getting something like
map object at 0x7fe8201bddf0, cast your map expression to a list like so:
list(map(... )).
houses = [('main st.', 4, 4000), ('elm st.', 1, 1200), ('pine st.', 2, 1600)]
Sort the list in the following ways:
A good way to quickly test your code is by running it in the terminal. Open a new terminal and type either
python3 or py to start the Python interpreter. Then you can enter code line-by-line
and see the results:
$ python3
>>> houses = [('main st.', 4, 4000), ('elm st.', 1, 1200), ('pine st.', 2, 1600)] # define houses list
>>> list(sorted(houses, reverse=True)) # sort in reverse alphabetical order
[('pine st.', 2, 1600), ('main st.', 4, 4000), ('elm st.', 1, 1200)] # see sorted result
A suggested extension for the Big Tweets Data problem from last week was to implement a
function called flat_counts(users), which takes in a nested
users dictionary and returns a new dictionary of hashtag counts, across all users. Here's an
example of what one of these flat counts dictionaries might look like:
counts = {'#cs106a': 9, '#Stanford': 2, '#yotter': 12, '#bit': 3, '#sand': 10, '#CSrocks': 7}
Recall the dictionary functions keys() and values() that
return a list of keys or values respectively:
>>> counts.keys()
['#cs106a', '#Stanford', '#yotter', '#bit', '#sand', '#CSrocks']
>>> counts.values()
[9, 2, 12, 3, 10, 7]
Write a function reverse_keys(counts) that prints the hashtags of a
counts dictionary in reverse alphabetical order (note how uppercase letters come after
lowercase ones, this is just how alphabetical sorting works):
reversed
#yotter
#sand
#cs106a
#bit
#Stanford
#CSrocks
Test your implementation by running python3 tweets.py -reverse small-tweets.txt in the
terminal.
In Python, there's a dictionary function items() that
returns a list of key-value tuples:
>>> counts.items()
[('#cs106a', 9), ('#Stanford', 2), ('#yotter', 12), ('#bit', 3), ('#sand', 10), ('#CSrocks', 7)]
Now, with help from the items() function,
implement a function, most_used(counts), which given a
counts dictionary, prints the top 5 most used hashtags with their counts, in order. For
example, most_used(counts) for the counts dictionary shown above would print:
most
#yotter 12
#sand 10
#cs106a 9
#CSrocks 7
#bit 3
Test your implementation by running python3 tweets.py -most small-tweets.txt
in the terminal.