#!/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" # new key-value pair d["Kermit"] = "banana" # new key-value pair d["Kermit"] = "orange" # re-seting the value associated with Kermit d["Miss Piggy"] = d["Kermit"] # add the key Miss Piggy to the dic # with the same value as what Kermit is # associated with 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") old_dict = {} # iterate 5 times, i will go from 0 to 4 for i in range(5): old_dict[i] = 0 print(old_dict) # {0:0, 1:0, 2:0, ...} print() # this code block from start to stop # start new_dict = {} for k in old_dict: new_dict[k] = old_dict[k] # stop print(new_dict) print(new_dict.copy()) 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) <---- most correct """ """ Translating code into a function --- (Using warm-up 2) """ def dict_copy(old_dict): """ Copies a dictionary. Parameters ---------- old_dict : dict mapping of keys to values. Returns ------- new_dict : dict contain all key/value pairs in old_dict. """ new_dict = {} for k in old_dict: new_dict[k] = old_dict[k] return new_dict d4 = {1: 10, 2:10} d5 = {"Felix": 97, "John": 43} d4_copy = dict_copy(d4) print(d4_copy) d5_copy = dict_copy(d5) print(d5_copy) print(d5_copy.items()) print() """ 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 types = [str, str, int, float] data_ls = data_utils.read_data("movies.csv", skip_header=True, type_casts = types) print(data_ls) print() """ 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 types = {"rotten_tomato": int, "IMDB": float} data_dicts = data_utils.read_data_dict("movies.csv", type_cast_dict=types) print(data_dicts) print() data_dicts = data_utils.read_data_dict("movies.csv") print(data_dicts) # How can you use the data utils get_column function with your list of dicts? """ Next time - string processing & string manipulation - data and data sources """