@extends('template') @section('title') Lab 10, Part 2: Student dictionaries @stop @section('content') # Lab 10, 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 names and years are accurate, but all the other information is fake. 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 'Tiffany': {'color': 'yellow', 'major': 'Political Science', 'movie': 'Avengers: End Game', 'year': '2022'}, 'Isabella': {'color': 'red', 'major': 'English', 'movie': 'Parasite', 'year': '2023'}, 'Miranda': {'color': 'aquamarine', 'major': 'Political Science', 'movie': 'Star Wars', 'year': '2023'}, ... } ``` {{--SPRING2020 removing sets, online course ## Task 0A. Build a movies set. 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). Note that Canopy prints the movies sorted alphabetically, but the set has no intrinsic order. ## Task 0B. Build a colors set. 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. Note that Canopy prints the colors sorted alphabetically, but the set has no intrinsic order. --}} ## Task 1A. Build a Majors dictionary. 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. {{-- **DISCLAIMER: your dictionary will have different counts, this is just to give you a general idea of what your dictionary will look like** --}} ```py In []: majorsDict = makeMajorsCountDict(studentDict) In []: majorsDict {'Bio': 5, 'CAMS': 3, 'CS': 7, 'East Asian Studies': 6, 'Economics': 7, 'English': 7, 'Japanese': 8, 'MAS': 11, 'Math': 6, 'Philosophy': 10, 'Physics': 6, 'Political Science': 7, 'Spanish': 3} ``` ## Task 1B. Create list of tuples from `majorsDict` Write a function called `extractTups` that takes a dictionary and returns a list of tuples in the form (`major`, `count`). Dictionaries do not have order, so the tuples will come out unordered. **Challenge**: use a list comprehension to generate your list of tuples ** Remember that the numbers below may differ from your counts** ```py In []: majorTups = extractTups(majorsDict) In []: majorTups Out[]: [('Physics', 6), ('Economics', 7), ('Japanese', 8), ('Political Science', 7), ('East Asian Studies', 6), ('Spanish', 3), ('Bio', 5), ('English', 7), ('Math', 6), ('CAMS', 3), ('MAS', 11), ('CS', 7), ('Philosophy', 10)] ``` ## Task 1C. Sort the tuples ### Task a. Sort tuples alphabetically by major
A short aside about SORTING
There are two built-in python functions (click on each word for documentation): sorted and sort. 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. We do not want to change our lists here, so sorted is the best one to use here.
```py [('Bio', 5), ('CAMS', 3), ('CS', 7), ('East Asian Studies', 6), ('Economics', 7), ('English', 7), ('Japanese', 8), ('MAS', 11), ('Math', 6), ('Philosophy', 10), ('Physics', 6), ('Political Science', 7), ('Spanish', 3)] ``` ### 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 [('Spanish', 3), ('CAMS', 3), ('Bio', 5), ('Physics', 6), ('East Asian Studies', 6), ('Math', 6), ('Economics', 7), ('Political Science', 7), ('English', 7), ('CS', 7), ('Japanese', 8), ('Philosophy', 10), ('MAS', 11)] ``` ## Task 2. Color dictionary with student list values 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': ['Isabella', 'Madeline', 'JJ', 'Audrey', 'Alexandria', 'Judy', 'Courtney', 'Katelyn', 'Yujun', 'Laura', 'Becky', 'Megan'] 'plaid': [ # list of student names here ], ... } ``` ## Task 3. Nested dictionaries. 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': [{'major': 'English', 'movie': 'Parasite', 'name': 'Isabella', 'year': '2023'}, {'major': 'Physics', 'movie': 'Harry Potter', 'name': 'Madeline', 'year': '2023'}, {'major': 'Math', 'movie': 'Star Wars', 'name': 'JJ', 'year': '2022'}, {'major': 'Math', 'movie': 'Harry Potter', 'name': 'Audrey', 'year': '2023'}, {'major': 'CS', 'movie': 'Bohemian Rhapsody', 'name': 'Alexandria', 'year': '2021'}, {'major': 'Bio', 'movie': 'Black Panther', 'name': 'Judy', 'year': '2023'}, {'major': 'English', 'movie': 'Black Panther', 'name': 'Courtney', 'year': '2021'}, {'major': 'Math', 'movie': 'Green Book', 'name': 'Katelyn', 'year': '2023'}, {'major': 'English', 'movie': 'Avengers: End Game', 'name': 'Yujun', 'year': '2023'}, {'major': 'Physics', 'movie': 'Avengers: End Game', 'name': 'Laura', 'year': '2023'}, {'major': 'Political Science', 'movie': 'Get Out', 'name': 'Becky', 'year': '2023'}, {'major': 'Philosophy', 'movie': 'Parasite', 'name': 'Megan', 'year': '2020'}] 'plaid': [ # list of dictionaries here ], ... } ``` @include('/labs/lab10/_toc') @stop