Lists of Dicts and For-loop Refresher¶
In this exercise we'll refresh ourselves on dealing with list
s, dict
s, and for
-loops.
I've included a dataset with information from the form on the first day.
- This dataset is a
list
where each entry isdict
. - Each entry has three key-value pairs:
"name"
"month"
"day"
Assignment¶
- Calculate the average
"month"
that someone in our class was born. - Calculate the averge
"day"
that someone in our class was born.
Bonus
- Calculate the average birthday for the year (assume it's not a leap year)
Reminders¶
Accessing an entry by index:
data[0] #=> {"name": 'Mr. P', "month": 4, "day": 26},
Accessing a specific value from the dict:
entry = data[0]
entry["month"] #=> 4
Accessing a specific value from the list-of-dicts in one-line:
data[0]["month"] #=> 4
Iterating over the entries with a for
-loop
# This for-loop prints out everyone's name
for entry in data:
print(entry["name"])
Calculating an average:
values = [1,2,3,4,5,6]
total = 0
count = 0
for val in values:
total += val
count += 1
avg = total / count
In [1]:
Copied!
data = [
{"name": 'Mr. P', "month": 4, "day": 26},
{"name": 'Keitaro', "month": 10, "day": 18},
{"name": 'Penelope', "month": 3, "day": 18},
{"name": 'Ayaan', "month": 1, "day": 19},
{"name": 'Ethan', "month": 3, "day": 9},
{"name": 'Jayden', "month": 7, "day": 27},
{"name": 'David', "month": 11, "day": 17},
{"name": 'Daniel', "month": 5, "day": 30},
{"name": 'Minhee', "month": 11, "day": 24},
{"name": 'Christopher', "month": 4, "day": 23},
{"name": 'Claire', "month": 6, "day": 5},
{"name": 'Jiya', "month": 8, "day": 23},
{"name": 'Yujie', "month": 3, "day": 25},
{"name": 'Florence', "month": 7, "day": 11},
{"name": 'Kevin', "month": 12, "day": 9},
{"name": 'Tony', "month": 10, "day": 17},
{"name": 'Deniz', "month": 4, "day": 10},
]
def average_of_dict_field(data, key):
"""
Calculates the average of a field defined
by "key" in the list-of-dicts, data.
"""
total = 0
count = 0
for entry in data:
month = entry[key]
total += month
count += 1
avg = total / count
return avg
avg_day = average_of_dict_field(data, "day")
avg_month = average_of_dict_field(data, "month")
print(f"The average month was {avg_month} and the average day was {avg_day}")
data = [
{"name": 'Mr. P', "month": 4, "day": 26},
{"name": 'Keitaro', "month": 10, "day": 18},
{"name": 'Penelope', "month": 3, "day": 18},
{"name": 'Ayaan', "month": 1, "day": 19},
{"name": 'Ethan', "month": 3, "day": 9},
{"name": 'Jayden', "month": 7, "day": 27},
{"name": 'David', "month": 11, "day": 17},
{"name": 'Daniel', "month": 5, "day": 30},
{"name": 'Minhee', "month": 11, "day": 24},
{"name": 'Christopher', "month": 4, "day": 23},
{"name": 'Claire', "month": 6, "day": 5},
{"name": 'Jiya', "month": 8, "day": 23},
{"name": 'Yujie', "month": 3, "day": 25},
{"name": 'Florence', "month": 7, "day": 11},
{"name": 'Kevin', "month": 12, "day": 9},
{"name": 'Tony', "month": 10, "day": 17},
{"name": 'Deniz', "month": 4, "day": 10},
]
def average_of_dict_field(data, key):
"""
Calculates the average of a field defined
by "key" in the list-of-dicts, data.
"""
total = 0
count = 0
for entry in data:
month = entry[key]
total += month
count += 1
avg = total / count
return avg
avg_day = average_of_dict_field(data, "day")
avg_month = average_of_dict_field(data, "month")
print(f"The average month was {avg_month} and the average day was {avg_day}")
The average month was 6.411764705882353 and the average day was 18.294117647058822
In [9]:
Copied!
from math import floor
days_per_month = {
1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31
}
def month_and_day_to_doy(month, day):
doy = 0
i = 1 # January
while i < month:
doy += days_per_month[i]
i += 1
doy += day
return doy
def doy_to_month_and_day(doy):
i = 1
while days_per_month[i] < doy:
doy -= days_per_month[i]
i += 1
month = i
days = doy
return month, days
total = 0
count = 0
for entry in data:
doy = month_and_day_to_doy(entry["month"], entry["day"])
total += doy
count += 1
avg_doy = floor(total / count)
month, day = doy_to_month_and_day(avg_doy)
print(f"The average birthday is {month}/{day}")
from math import floor
days_per_month = {
1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31
}
def month_and_day_to_doy(month, day):
doy = 0
i = 1 # January
while i < month:
doy += days_per_month[i]
i += 1
doy += day
return doy
def doy_to_month_and_day(doy):
i = 1
while days_per_month[i] < doy:
doy -= days_per_month[i]
i += 1
month = i
days = doy
return month, days
total = 0
count = 0
for entry in data:
doy = month_and_day_to_doy(entry["month"], entry["day"])
total += doy
count += 1
avg_doy = floor(total / count)
month, day = doy_to_month_and_day(avg_doy)
print(f"The average birthday is {month}/{day}")
The average birthday is 6/30
In [ ]:
Copied!