#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 10/25/2022 DS 2000 Lecture 14 - dictionaries, part 1 Logistics: - Homework 6 is due Friday @ 9pm - Dictionaries are not required. We don't think that using them will make your lives easier. - NO Quiz this week - Friday lecture materials will be released for asynchronous viewing: - ____________ - *IMPORTANT* On Friday, I will be holding office hours during from 9:50 - 10:55am and 1:35 - 2:40pm in RI 236. No class or office hours during Section 4's regular meeting time. (I will be on a train.) - remote attendance (https://bit.ly/remote-ds2000-muzny) Three ways to participate (please do one of these!) 1) via the PollEverywhere website: https://pollev.com/muzny 2) via text: text "muzny" to the number 22333 to join the session 3) via Poll Everywhere app (available for iOS or Android) """ """ Warm-up 1: If I have the following code, what will the output be? """ """ A. [0, 1, 2, 3, 4] B. [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] C. [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]] D. [0, 1, 2] E. Error """ """ Warm-up 2: If I have the following code, what will it's effect be? """ file = open("assumethisfileexists.txt", "r") for line in file: if line[0] != "%": print(line) else: line = file.readline() file.close() """ A. This will print all lines in the file B. This will print all lines in the file that don't start with % C. This will print no lines in the file D. This will do something else E. Error """ """ Dictionaries --- A dictionary is a *data structure*, similarly to lists. A dictionary links a *key* to a *value*. Examples: student emails -> graduation year words -> definitions words -> counts of how often they occur names -> ages Keys: any immutable data type (ints, floats, strings, booleans) keys are *unique* (no key can occur more than once) Values: any data type values are not unique """ # # Dictionary examples # # Creating a dictionary # # an empty dictionary # print("dict examples") # ages = {} # # a dictionary w/ some starting values # ages = {"Felix": 31, "Dylan": 33} # print(ages) # # number of key-value pairs # print(len(ages)) # # adding a key/value pair to a dictionary # ages["Donald Duck"] = 5 # print(ages) # # number of key-value pairs # print(len(ages)) # # updating a key/value pair in a dictionary # ages["Felix"] = 32 # ages["Felix"] = ages["Felix"] + 1 # ages["Felix"] += 1 # print(ages) # # number of key-value pairs # print(len(ages)) # # what if I look up a key/value that doesn't exist? # # KeyError # # print(ages["Lizzo"]) # # test for a key in a dict # if "Lizzo" in ages: # print(ages["Lizzo"]) # else: # print("oh no!") # print() # # iterating through all values in a dictionary # for key in ages: # print(key) # key # print(ages[key]) # value it is linked to # print() # for key, value in ages.items(): # # key = next key in the dict # # value = ages[key] # print(key) # key # print(value) # value it is linked to # print() """ How is a list similar to a dictionary? --- """ """ Writing a function ---- Write a function, count_words, that takes in a string text as input and returns a dictionary of word counts. """ def count_words(text): """ Count each word in a given string. Parameters ---------- text : str words separated by whitespace. Returns ------- dict of word counts. """ print("to be implemented") test1 = "This is a sentence." test2 = "hat hat hat cat bat hat" """ Writing a larger program --- Using the movie review data (blackadam.txt and tickettoparadise.txt), write a larger program that will read in each review for the given movie, line-by-line and does two things: 1) Lets the user ask how many times a word exists in the reviews 2) Reports the combined word counts for all reviews for that movie https://www.rottentomatoes.com/m/black_adam https://www.rottentomatoes.com/m/ticket_to_paradise_2022 """ """ Cool-down 1 --- Go pull up the HW 6 write-up. What cleaning must be done to the data that you are working with? """ """ Next time (what to expect for the asynchronous installment) - combining dictionaries with lists - writing another, larger program! """