@extends('template') @section('title') Lab 11, Part 2: Student dictionaries @stop @section('content') # Lab 11, Part 2: Student dictionaries Create a new file called `students.py`. At the top of your file, add this line so you can access the `studentDict` dictionary: ```py from studentDict import studentDict ``` In the file `studentDict.py`, we are providing you with a randomly-generated CS111 student dictionary called `studentDict`. The dictionaries are entirely made up. You can open the file in canopy and look at the contents. Here is a peek at some of the dictionary contents. In this lab, we will practice manipulating a dictionary to create new dictionaries. ```py 'Eunji': {'major': 'Spanish', 'color': 'chartreuse', 'movie': 'Avengers: End Game', 'year': '2024'}, 'Amal': {'major': 'Political Science', 'color': 'yellow', 'movie': 'Parasite', 'year': '2025'}, 'Jocelyn':{'major': 'Philosophy', 'color': 'aquamarine', 'movie': 'Da 5 Bloods', 'year': '2025'}, ... } ``` {{--SPRING2020 removing sets, online course ## Task 0A. Build a movies set.
Partner B
Write a function called `buildMovieSet` that takes a dictionary parameter (eg `studentDict`) and returns a **set** with all the movies in it. Your resulting set should have 10 movies in it (hint: you can use `len` with sets). ## Task 0B. Build a colors set.
Partner A
Write a function called `buildColorSet` that takes a dictionary parameter (eg `studentDict`) and returns a **set** with all the favorite colors in it. Your resulting set should have 10 favorite colors in it. --}} ## Task 1A. Build a Majors dictionary.
Partner B
Write a function called `makeMajorsCountDict` that takes a dictionary parameter (eg `studentDict`) returns a dictionary with majors as the key and count of students with that major as the value. **Note**: Recall that our lab examples are shown for ease of readability; Thonny's display will be more condensed. {{-- **DISCLAIMER: your dictionary will have different counts, this is just to give you a general idea of what your dictionary will look like** --}} ```py >>> majorsDict = makeMajorsCountDict(studentDict) >>> majorsDict { 'Physics': 10, 'MAS': 10, 'History': 11, 'Philosophy': 11, 'Japanese': 4, 'CS': 7, 'CAMS': 5, 'Spanish': 8, 'East Asian Studies': 1, 'Math': 10, 'Economics': 3, 'Bio': 12, 'English': 5, 'Political Science': 4} ``` ## Task 1B. Create list of tuples from `majorsDict`
Partner A
Write a function called `extractTups` that takes a dictionary and returns a list of tuples in the form (`major`, `count`). {{--**Challenge**: use a list comprehension to generate your list of tuples--}} **Note** Your list of tuples may display differently. We are formatting the lists to maximize readability in this page. ```py >>> majorTups = extractTups(majorsDict) >>> majorTups [('Physics', 10), ('MAS', 10), ('History', 11), ('Philosophy', 11), ('Japanese', 4), ('CS', 7), ('CAMS', 5), ('Spanish', 8), ('East Asian Studies', 1), ('Math', 10), ('Economics', 3), ('Bio', 12), ('English', 5), ('Political Science', 4)] ``` {{-- ## Task 1C. Sort the tuples
A short aside about SORTING
There are two built-in python functions (click on each word for documentation): sorted and sort (there's also a brief tutorial on sorting that covers advanced details). In a nutshell, sorted returns a new sorted list, whereas sort changes the contents of the original list into a sorted list. When you use sort, the order of the original list is lost.
To keep things organized, add code to your `extractTups` function that sorts the results before returning them. By default, this will end up sorting based on the alphabetical ordering of the first letter of each major: ```py [('Bio', 12), ('CAMS', 5), ('CS', 7), ('East Asian Studies', 1), ('Economics', 3), ('English', 5), ('History', 11), ('Japanese', 4), ('MAS', 10), ('Math', 10), ('Philosophy', 11), ('Physics', 10), ('Political Science', 4), ('Spanish', 8)] ``` --}} {{-- Removed 2020-9-28 since we aren't covering sorting during terms... ### Task b. Sort tuples numerically by count Hint: a helper function is a good idea here. Example 3 on this page describes key functions. ```py [('East Asian Studies', 1), ('Economics', 3), ('Japanese', 4), ('Political Science', 4), ('CAMS', 5), ('English', 5), ('CS', 7), ('Spanish', 8), ('Physics', 10), ('MAS', 10), ('Math', 10), ('History', 11), ('Philosophy', 11), ('Bio', 12)] ``` --}} ## Task 2. Color dictionary with student list values
Partner A
Write a function called `makeColorDict` that takes a dictionary parameter (e.g. `studentDict`) and returns a dictionary with the color as the key and a **list** of the students' names with that color as the value. For example, here are the students who have the color red. This is not the entire dictionary, just a glimpse of one value for one key (you can scroll to the right in the window below to see the entire list of students who love red). ```py { 'blue': [ # list of student names here ], 'red': ['Sarah', 'Emilia', 'Rumi', 'Keiko', 'Youngja', 'Daisy', 'Trisha', 'Seana', 'Bryant'], 'plaid': [ # list of student names here ], ... } ``` ## Task 3. Nested dictionaries.
Partner B
Write a function called `makeColorMiniDict` that takes a dictionary parameter and returns a dictionary with the color as the key and **a list of dictionaries** as the value. Each dictionary in the list of dictionaries represents one student who has that color. Here is a snapshot of the list of dictionaries that is the value associated with the key value `red` (you can scroll horizontally <--> to see all the dictionaries). ```py { 'blue': [ #list of dictionaries here ], 'red': [{'movie': 'Black Panther', 'major': 'MAS', 'year': '2023', 'name': 'Sarah'}, {'movie': 'Frozen', 'major': 'CS', 'year': '2023', 'name': 'Emilia'}, {'movie': 'Da 5 Bloods', 'major': 'Bio', 'year': '2022', 'name': 'Rumi'}, {'movie': 'Avengers: End Game', 'major': 'English', 'year': '2024', 'name': 'Keiko'}, {'movie': 'Parasite', 'major': 'MAS', 'year': '2024', 'name': 'Youngja'}, {'movie': 'Black Panther', 'major': 'History', 'year': '2023', 'name': 'Daisy'}, {'movie': 'Avengers: End Game', 'major': 'English', 'year': '2024', 'name': 'Trisha'}, {'movie': 'Avengers: End Game', 'major': 'Physics', 'year': '2024', 'name': 'Seana'}, {'movie': 'Star Wars', 'major': 'Bio', 'year': '2025', 'name': 'Bryant'}], 'plaid': [ # list of dictionaries here ], ... } ``` ## Optional Challenge: Multiple layers.
Partner A
If you've still got time left in the lab, write a function called `makeMajorClassDict` that organizes the data into a dictionary > dictionary > list-of-dictionaries structure where the outer keys are majors, the inner keys are class years, and the final values are lists of full student dicts (still including major and class year). Here's an example of what part of that might look like: {{--TODO: These examples are missing colors! --}} {{--TODO: These examples are also just wrong! --}} ```py { 'Bio': { # dictionary here }, 'English': { '2024': [ {'movie': 'Avengers: End Game', 'major': 'English', 'year': '2024', 'name': 'Trisha'}, {'movie': 'Avengers: End Game', 'major': 'English', 'year': '2024', 'name': 'Keiko'}, ], '2023': [ {'movie': 'Black Panther', 'major': 'English', 'year': '2023', 'name': 'Sarah'}, ] }, 'Physics': { # dictionary here }, ... } ``` @include('/labs/lab11/_toc') @stop