#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 10/85/2022 DS 2000 Lecture 15 - dictionaries, part 2 (the asynchronous lecture) 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 - *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.) """ """ Warm-up 1: dictionaries Given the following code, what will the output be? """ print("warm up 1") d = {} d["Oscar"] = "apple" d["Kermit"] = "banana" d["Kermit"] = "orange" d["Miss Piggy"] = d["Kermit"] print(len(d)) print(d) print() """ A. 1 B. 2 C. 3 D. 4 E. Error """ """ Warm-up 2: dictionaries If I have the following code, what is the effect of the designated code block? """ print("warm up 2") d2 = {} for i in range(5): d2[i] = 0 # this code block from start to stop # start d3 = {} for k in d2: d3[k] = d2[k] # stop print(d3) print() """ A. This will create an empty dictionary B. This will remove all values from d2 C. This will set all values in d3 (new_dict) to 0 D. This will put the same keys in d3 that d2 has E. This will create a copy of d2 (old_dict) """ """ Translating code into a function --- (Using warm-up 2) """ """ Dictionaries --- Essential patterns: 1) testing for a key in/not a dictionary key in dict "Felix" in d5_copy -> True or False 2) iterating through a dictionary for key in dict: # code to be repeated for key, value in dict.items(): # code to be repeated 3) adding keys/values to dictionaries (especially in the context of loops) dict[key] = value """ """ Review: 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 three 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 """ """ Example data for the rest of lecture: title,MPAA,rotten_tomato,IMDB Black Adam,PG-13,39,7.1 Ticket to Paradise,PG-13,56,6.3 Smile,R,79,6.9 Halloween Ends,R,41,5.0 The Woman Kind,PG-13,94,6.8 """ """ Revisiting data utils --- To-do: update the read_data function so that you can specify types for each column. Suggestion: you may find it helpful to write a function that will get a subset of rows for you based on the value in a specific column :) """ import data_utils # read in the data as a list of lists """ Dictionaries + csv files --- So far, we've read in data as lists of lists. We can also read in data as lists of dictionaries! """ # write a function to read in data from a csv file as a list of dicts """ Next time - string processing & string manipulation - data and data sources """