Recitation 5 — Lists
Skills: 3
Reading: 5.1, 5.2
Working with Lists
Here's some cafe sales data:
cafe-data = table: day, drinks-sold
row: "Mon", 45
row: "Tue", 30
row: "Wed", 55
row: "Thu", 40
row: "Fri", 60
end
Extracting Lists from Tables
Extract the drinks-sold column as a list using get-column
. What does this list look like? How is it different from the original table?
Mathematical Operations on Lists
You'll need to import the math and statistics libraries:
import math as M
import statistics as S
Using the extracted list, find:
- The maximum number of drinks sold (use
M.max
) - The minimum number of drinks sold (use
M.min
) - The average daily sales (use
S.mean
) - The total weekly sales (use
M.sum
)
Creating List Literals
Create these lists directly (without using a table):
- A list of test scores: 85, 92, 78, 96, 88, 91
- An empty list
- A list of names: "Alice", "Bob", "Carol"
Using your test scores list, calculate:
- The highest score (use
M.max
) - The average score (use
S.mean
) - The total of all scores (use
M.sum
)
Transforming Lists with Map
You'll need to import the lists library:
import lists as L
Convert these Celsius temperatures to Fahrenheit: 0, 10, 20, 30, 40
The formula is: (celsius × 9/5) + 32
Use L.map
with a function that applies this formula to each temperature.
Working with Student Grades
Given these raw scores: 78, 85, 92, 67, 88, 94, 73, 81, 90, 86
Apply a 5-point curve to all scores using L.map
with a lambda function that adds 5 to each score.
Then convert the curved scores to letter grades using L.map
with a lambda function that checks the score ranges:
- A: 90 and above
- B: 80-89
- C: 70-79
- D: 60-69
- F: below 60
How many students got each letter grade?
Finding Unique Values
Use L.distinct
to find all the unique letter grades that were assigned.
Filtering Lists
Use L.filter
with a lambda function to find all the passing scores (60 and above) from your curved scores list. How many students passed?
Wrap-Up
When would you use a list instead of a table? What real-world data could you analyze using these list operations?