CSCI 1100 - Lecture 17
Chapter Thirty-One Lecture 17 — Dictionaries, Part 2
31.1 Overview
This section builds upon previous knowledge regarding dictionaries in Python, emphasizing their versatile applications in coding scenarios.
Examples are drawn from the Internet Movie Database (IMDB) to illustrate real-world usage of dictionaries.
Key topics include the creation of dictionaries using string/set pairs, converting dictionaries with single keys into more complex structures, and methods for merging information from multiple dictionaries for enhanced data manipulation.
Emphasis on the significance of structure in storing attribute/value pairs effectively.
Explores how accessing APIs can yield data in dictionary format, facilitating easy data retrieval.
31.2 Recap of Dictionaries
Structure Comparison:
A detailed comparison between lists and dictionaries highlights how dictionaries allow for any hashable type as indices (keys).
Example:
List:
listoption = ['a','b','c']Dictionary:
dictoption = {0:'a', 1:'b', 2:'c'}
Access and Update:
Access & Update Examples:
listoption[1]returns 'b' whiledictoption[1]also returns 'b', showcasing their overlapping functionalities.Updating is straightforward:
listoption[1] = 'd'updates the list,dictoption[1] = 'd'updates the dictionary equivalent.
Extension Concept:
Attempting
listoption[10] = 'e'results in an error due to the list's fixed size, whiledictoption[10] = 'e'is valid as dictionaries can expand dynamically.
Key Versatility:
Dictionaries support keys of varying hashable types, including strings and tuples.
Examples:
d = {'Gru': 3, 'Margo': 4}signifies individuals with their associated counts.
Data Extraction:
Efficient data extraction can be achieved using iteration.
For
d2 = {'Gru': set([123, 456]), 'Margo': set([456])}, keys can be accessed seamlessly usingd2.keys(), promoting easier data handling.
31.3 Copying and Aliasing Dictionaries
Understanding Copies:
This section clarifies the concept of copies in Python, differentiating between shallow and deep copies.
Example: A shallow copy maintains references to the original items, whereas a deep copy replicates every item recursively.
Code examples illustrate how dictionaries, along with lists and sets, manage alias relationships during assignment, highlighting potential pitfalls in data manipulation.
31.4 Back to IMDB: Values as Sets
This subsection exemplifies the practical use of dictionaries within IMDB data structures, showcasing how they efficiently store multiple entries for individual actors across various movies.
Storing data as sets is crucial for this use case to avoid duplication and maintain uniqueness among movie entries per individual.
31.5 Converting to Other Dictionaries: The Busiest Individuals
Aim:
The objective is to identify the top 10 or 25 individuals engaged in the most unique movie roles.
This involves utilizing integers as keys (distinct counts of movies) and using lists of actor names as values.
Building this dictionary requires extending existing program structures to capture this data effectively.
It is also important to access the dictionary in reverse order to retrieve and print names of the busiest individuals efficiently.
31.6 More from IMDB
Secondary Dictionary Construction:
This approach involves setting up a secondary dictionary that uses movie titles as keys and sets of actors as values.
This structure aids in conducting analyses to derive insights such as determining the movies with the largest cast and examining specific instances such as Meryl Streep's filmography or the Kevin Bacon degrees problem.
Definitions for Connections:
Degree 1, 2, and 3 connections define how closely actors are linked based on their collaborative performances.
31.7 Attribute / Value Pairs
Complex Data Structures:
The chapter highlights the incorporation of dictionaries within dictionaries and other data structures such as lists.
Example: A real estate listing dictionary might contain attributes like:
Reference number
Address
Number of bedrooms
Price point
Pool availability
Architectural style and age.
An exercise suggests implementing code to filter properties based on specified criteria, enhancing practical coding skills.
31.8 Accessing APIs
API Data Retrieval:
Many APIs offer data in JSON format that can be easily decoded into Python dictionaries.
Best practices emphasize using libraries such as
urllibandjsonfor efficient API communications.Example: Accessing Nominatim API for geolocation data through an easily structured URL.
It’s vital to navigate authentication processes for certain APIs, but once authenticated, the methodology for data access remains consistent.
31.9 Final Example and Summary
Final Example Exercise:
This involves transforming a dictionary based on hobbies into one that accurately lists individuals associated with each hobby.
Summary Points:
Recapitulation of the usage of dictionaries for managing datasets through the use of sets, implementing number-based keys, and techniques for extracting data from IMDB.
Reinforces the importance of utilizing dictionaries effectively for attribute/value storage and accessing external APIs.
31.10 Additional Dictionary Practice Problems
Develop a favorite colors dictionary and manipulate it.
Identify the most preferred color by tallying counts using a secondary dictionary.
Create a solution that sorts movie counts.
Assess film counts yearly, determining which year had the highest production rate.
Analyze occurrences of last names among IMDB data to determine common names.
Implement intelligent looping to discover the two individuals with the most shared movie appearances.