# Dictionaries --- CS 65 // 2021-03-30 ## Administrivia - Midterm course evaluations + Please take the time to fill out [this anonymous questionnaire](https://docs.google.com/forms/d/e/1FAIpQLSeF4te7AXCmSb2tsFGi_YaKpsxHaGUtwtAj_Vrol0J5a6B1bA/viewform?usp=sf_link) - Still grading Assignment 4 and Exam 2 ## Extra Credit Activities - [Drake's Got Talent](https://calendar.drake.edu/event/drakes_got_talent?utm_source=newsletter&utm_medium=email&utm_content=https%3A//d31hzlhk6di2h5.cloudfront.net/20210330/37/d7/7f/18/d0461f71e982682339189262_352x354.jpeg&utm_campaign=Staff-March-30#.YGJQSGRKg-Q) + Thursday, April 1 at 5pm + Helmick Commons - Drake Relays + Relays Reveal 2021 * Wednesday, March 31st at 6pm * Olmsted Center in the Parents Hall + Other events during the week of April 12 to 24th ## Assignment 6 - Questions about anything? # Associative Data ## Associative Data - Lists allow us to store **sequential data** + i.e. data that can be strictly ordered - The strict ordering of the elements in a list allows us to easily access elements **by their index** + `t[0]`, `t[1]`, `t[2]`, ... - A list gives us a mapping of **indices** to values - What if we want to map **strings** to values? ## Associative Data Examples - A **dialer app** on your cell phone + Want to store contact information + Want to **lookup** phone numbers by **names** + Want to *add*, *remove*, and *change* contact info - A **library app** that maps author names to books they've written - A **student meal plan app** that maps a Drake ID to the number of meals / Bulldog Bucks remaining # Dictionaries ## The `dict` Type - **Purpose** + To conveniently manipulate associative data - **Express** + `{}`, `dict()` + `{key1:value1, key2:value2, ...}` ## The `dict` Type - **Operations** + Lookup values by their key + Change values by their key + Deleted key/value pairs + Number of key/value pairs ## Self Check 1 1. Why can't we use a `list` object as a key in a dictionary? 2. How can we iterate over a dictionary with a `for` loop? ## Dictionary Exercise - Create a `dict` that maps **email addresses** to **names** + Eric Manley (eric.manley@drake.edu) + Meredith Moore (meredith.moore@drake.edu) - Write an expression that adds an entry for + Adam Case (adam.case@drake.edu) - Delete the entry for **Eric Manley** - Write a loop that iterates over all the key/value pairs and prints them ## Self Check 2 - Write a function called `has_value(d, value)` that takes two parameters and returns `True` if the dictionary `d` contains the value `value` and returns `False` otherwise. # Word Frequencies ## Word Frequencies - Suppose we want to analyze the frequency of words an author uses in their books - Let's write a function that constructs a dictionary that maps each word in a file to the number of times it appears in the text - We can then run our program on various books on **Project Gutenberg** ## Word Frequencies ```py def parse_words(filename): f = open(filename) list_of_words = [] for line in f: words_on_line = line.strip().split() for word in words_on_line: if word.isalpha(): list_of_words.append(word.lower()) f.close() return list_of_words ``` ## Word Frequencies ```py def count_words(list_of_words): freq = {} for word in list_of_words: if word in freq: freq[word] += 1 else: freq[word] = 1 return freq ``` # Hashtables ## Hashtables - Dictionaries are implemented a **hashtable** - These are beyond the scope of this course, but they provides very efficient access to operations like: + Add (key, value) pair + Lookup by key + Modify the value for a certain key + Delete (key, value) pairs - Stay tuned for CS 66 where you'll learn more! # Choosing Keys ## Choosing Good Keys - Suppose we want to store some associative data - What should be the "key" of the data? + **books**: title, author, ISBN, year, publisher + **players**: name, uniform number, team, position + **computers**: manufacturer, model number, processor, memory, disk size - Are some "key" choices better than others? Why or why not? ## Choosing Good Keys - **books**: title, author, ISBN, year, publisher - **players**: name, uniform number, team, position - **computers**: manufacturer, model number, processor, memory, disk size ## Choosing Good Keys - Keys should be **convenient** for your application - Keys should **uniquely identify** the entry---otherwise key collisions might occur causing data to be overwritten! - Keys should be **immutable**---this helps ensure that lookups are consistent - Don't use graphics objects as keys! # Assignment Time