-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh_low.py
More file actions
42 lines (33 loc) · 1.23 KB
/
Copy pathhigh_low.py
File metadata and controls
42 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import json
import statistics
def main():
file = open("weather.json")
text = file.read()
weather_dict = json.loads(text)
find_highest_average(weather_dict)
print()
find_lowest_average(weather_dict)
def find_highest_average(weather_dict):
# list of average high temp for each city
averages = []
for city in weather_dict:
highs_list = [day["high"] for month in weather_dict[city].keys() \
for day in weather_dict[city][str(month)]]
high_mean = statistics.mean(highs_list)
averages.append((city, high_mean))
# Sort on 2nd element in tuple with lambda function
averages.sort(key=lambda elem: elem[1])
print("Highest average high")
print(averages[-1][0] + ":", str(averages[-1][1]))
def find_lowest_average(weather_dict):
averages = []
for city in weather_dict:
lows_list = [day["low"] for month in weather_dict[city].keys() \
for day in weather_dict[city][str(month)]]
low_mean = statistics.mean(lows_list)
averages.append((low_mean, city))
# Default is to sort on 1st element in tuple
averages.sort()
print("Lowest average low")
print(averages[0][1] + ":", str(averages[0][0]))
main()