Write a function hottest_city (csv_filename) that analyses the temperatures reco
ID: 3588529 • Letter: W
Question
Write a function hottest_city (csv_filename) that analyses the temperatures recorded in a CSV file, and returns a 2-tuple made up of the maximum temperature in the whole dataset along with a sorted list of the names of cities where that temperature was recorded The first column of the CSV file will contain the city name. The rest of the columns will be months of the year. The first row of the CSV files will provide column headings. Here is an example file (with an incomplete set of months): max temp_tiny.cSV city/month,Jan, Feb,Mar,Apr Melbourne,41.2,35.5,37.4,29.3 ,31.3,40.2,37.9,29 Darwin, 34,34,33.2,34.5 Here is an example of how hottest city should work: pr1nt (hottest_c1ty('max_temp_tiny.csv)) (41.2, ['Melbourne'])Explanation / Answer
import csv def hottest_city(csv_filename): fp = open(csv_filename) data = csv.reader(fp) header = next(data) content = list(data) for i in range(0, len(content)): for j in range(1, len(content[i])): content[i][j] = float(content[i][j]) maxcity = [] for i in range(0, len(content)): maxcity.append(max(content[i][1:])) maxaus= max(maxcity) cities=[] for i in range(0, len(content)): for j in range(1, len(content[i])): if maxaus == content[i][j]: cities.append(content[i][0]) cities = sorted(cities) pair = (maxaus, cities) return pair
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.