Lecture 21 Slides - Dictionary Uses

1 of 12

Dictionaries for Tallying and Counting

1

CS 110

2 of 12

Reminders and Announcements

Announcements

  • P1 Grades Posted Early Next Week (keep in mind these are graded by hand!)
  • Q2 Grades available tonight
  • Common End of Quarter Questions post on edSTEM!

This Week

  • Monday - Dictionary Algorithms
  • Wednesday - (Pre-recorded / MQ) Reading from Files + Tutorial 6
  • Friday - Libraries and APIs (MQ) + Ex 7 Due

3 of 12

Dictionaries (Cheat Sheet) - 01_dictionary_review.py

3

Create a dictionary

my_dict = {}

Get element from a dictionary

my_dict.get(key)

Add/replace items in dictionary

my_dict[key] = value

Remove items from dictionary

my_dict[key].pop(index)

Iterate through a dictionary

for key in my_dict:� print(key, my_dict[key])

Check if key exists in dictionary

if my_dict.get(key) is None…

if key in my_dict…

4 of 12

Dictionaries: When are they useful?

  1. Lookups and crosswalksExamples: language translation; lookup state capitals
  2. Representation�Useful for representing complex entities via heterogeneous dictionaries�Examples: YouTube data (title, user, comments, etc.)
  3. Counting and grouping�Useful for tallying, counting, and assigning information to categories�Examples: how many violations per restaurant; how many representatives per state

4

5 of 12

Example: Creating a Dictionary

eng2sp = {

'one': 'uno',

'two': 'dos',

'three': 'tres'

}

5

Keys�(left)

Values�(right)

  • Dictionaries store a series of key/value pairs
  • The order of the key / value pairs is unpredictable. It may or may not print in the same order as you see above (but it may).

6 of 12

Complex Lookup tables and crosswalks

Challenge: Write a program that allows users to either lookup a French translation for an English word, or add a new French translation.

6

7 of 12

Dictionaries: When are they useful?

  • Lookups and crosswalksExamples: language translation; lookup state capitals
  • Representation�Useful for representing complex entities via heterogeneous dictionaries�Examples: YouTube data (title, user, comments, etc.)
  • Counting and grouping�Useful for tallying, counting, and assigning information to categories�Examples: how many violations per restaurant; how many representatives per state

7

8 of 12

8

9 of 12

Example: One Video → Dictionary

single_video = {

"title": "Rick Astley - Never Gonna Give You Up",

"channel": {

"name": "Rick Astley",

"subscribers_count": 4460000

},

"description": "The official video for Never Gonna Give You Up by Rick Astley",

"date_posted": "Nov 1, 2010",

"id": "dQw4w9WgXcQ",

"like_count": 18000000,

"comment_count": 2420791,

"comments": [

{"user": "YouTube", "text": "can confirm: he never gave us up"},

{"user": "JB_OldVoltBike", "text": "I got rickrolled by a link saying it got taken down."}

]

}

10 of 12

Dictionaries: When are they useful?

  • Lookups and crosswalksExamples: language translation; lookup state capitals
  • Representation�Useful for representing complex entities via heterogeneous dictionaries�Examples: YouTube data (title, user, comments, etc.)
  • Counting and grouping�Useful for tallying, counting, and assigning information to categories�Examples: how many violations per restaurant; how many representatives per state

10

11 of 12

Dictionary counting algorithms

Dictionaries also help us to count and group things. Examples:

  • Number of instances of each letter in a word
  • Number of restaurants with the most health violations (order by those who are the worst offenders)
  • The basic idea is to loop through a sequence of data and…
    • If we haven't seen that piece of data before, then we add it to our dictionary with a unique key and value of 1 (in other words "we've seen this piece of data once"
    • If we have seen that data before (i.e. it's already in our dictionary as a key), then we increment the count for that key

11

12 of 12

Tallying and Counting

How many times does each letter in the word supercalifragilisticexpialidocious appear?

The algorithm:

  • Create an empty dictionary
  • Loop through each letter in the word
  • If the letter is not in the dictionary, add a new entry to the dictionary
  • Otherwise, increment the existing dictionary entry by 1.

03_count_letters.py & Python Tutor