Lecture 21 Slides - Dictionaries to Represent and Count

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 Wednesday at the latest
  • 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

del my_dict[key]

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?

  • 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

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": "Cat Vibing To Ievan Polkka (Official Video HD) Cat Vibing To Music | Cat Vibing Meme",

"channel": {

"name": "Bilal Göregen",

"subscribers_count": 15800000

},

"description": "Cat Vibing To Ievan Polkka - Street Drummer With Vibing Cat Meme (full video) New Cat Meme Cat Vibing To Ievan Polkka (Official Video HD) Cat Vibing To Music | Cat Vibing Meme",

"date_posted": "Nov 1, 2020",

"url": "https://www.youtube.com/watch?v=NUYvbT6vTPs",

"like_count": 3200000,

"comment_count": 144634,

"comments": [

{"user": "JonJaded", "text": "The meme, the music, the guy, the cat...everything is perfect."},

{"user": "randomcradevr", "text": "A turkish man playing a Finiish song while a Canadian cat vibes."}

]

}

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