in python now we will read in the data store it in a dictionary of lists data struct 5153750
IN PYTHON
Now we will read in the data, store it in a dictionary of lists data structure, and then let the user type in names. If a name appears in our data, we will draw a graph, like the one below, showing how the popularity of the name evolved since 1880.
You will begin by completing the function make_name_dict that reads in the counts file and populates a dictionary that can be used to look up a list of counts using the person's name. The counts should be stored as a list of integers, not strings. The function returns the dictionary.
Next complete the function get_counts_for_name . This function contains a loop that keeps asking the user to enter a name until it finds it in the dictionary. When “Name” is found it simply prints out “Found Name” on a single line followed by the Python list of counts. The exact format for the prompt and output is shown here:
What's your name? spam
What's your name? porsche
What's your name? Porsche
Found Porsche
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 10, 14, 9, 7, 19, 48, 64, 51, 45, 46, 57, 66, 82, 127, 182, 172, 159, 179, 123, 123, 70, 68, 45, 30, 35, 27, 12, 25, 32, 15, 20, 9, 15, 18, 17, 16, 11, 9, 14, 9, 5, 0, 13, 0, 5]
Strategy hints:
-
● The split method will give you a list of counts as strings, but you will need to convert them to integers before storing them in the dictionary.
-
● The format for the printed list is exactly what you get if your give a list to the print command. (e.g. print(counts) ) We will improve on this output in the next part.
import plainchart def main(): counts_by_name = make_name_dict() counts – get_counts_for_name(counts_by_name) # Uncomment for Part 4 # totals_by-year = get_totals_by-year # pct_counts – percentages(counts, totals_by_year) # Uncomment for Parts 3 and 4 # make_graph(counts) def make_name_dict(): return def get_counts_for_name(counts_by_name): return def make_graph(values): # Print the X axis print_x_axis def get_totals_by_year(): return def percentages (counts, totals_by_year): return # Prints the X axis for the chart def print_x_axis: line – for y in range(1880, 2017, 10): line – line + ” ” + str(y) + ” “* 5 print(line) main()