# Lab09 CS111 # More practice with dictionaries # Manipulations of kona triathlon data # Data is provided as a list of dictionaries in the file kona.py from kona import kona # Task 1 def printNames(data): """ Print all the athlete names """ for athlete in data: print(athlete['lastname'] + ',' + athlete['firstname']) # ---- Testing printNames ---- if __name__ == '__main__': print('\n\n===== Task 1. printNames =====') printNames(kona) # Tungesvik -> Kempson # Task 2 def printNamesCountry(data, country): """ Print the athlete names of a specified country """ for athlete in data: if athlete['country'] == country: print(athlete['lastname'] + ',' + athlete['firstname']) # ---- Testing printNameCountry ---- if __name__ == '__main__': print('\n\n===== Task 2. printNamesCountry =====') printNamesCountry(kona, 'FRA') # 5 names: # Tissot,Alexis # Mennesson,William # Philipps,Joachim # Filleul,Valentine # Pertsinidis,Nicolas # Task 3 def getAllCountries(data): """ Returns a list of unique countries from the provided data """ # initialize list to store all countries countries = [] for athlete in data: thisCountry = athlete['country'] # only add countries that are not already in the countries list if thisCountry not in countries: countries.append(thisCountry) return countries # ---- Testing getAllCountries ---- if __name__ == '__main__': print('\n\n===== Task 3. getAllCountries =====') x = getAllCountries(kona) print(x) # ['USA', 'SWE', 'GBR', 'CAN', [...], 'BRA', 'ARG', 'RUS', 'MEX', 'NOR'] print(len (x)) # 22 # Task 4 def totalAthletesByCountry(data): results = {} for athlete in data: country = athlete['country'] # Country already counted, increment if country in results: results[country] += 1 # Country not counted, initialize else: results[country] = 1 return results if __name__ == '__main__': print('\n\n===== Task 4. totalAthletesByCountry =====') print(totalAthletesByCountry(kona)) # Task 5 def buildDictWithAverageEventTimes(data): athletesByCountry = totalAthletesByCountry(data) countries = getAllCountries(data) results = {} # Initialize 0 totals for swim, bike, run for country in countries: results[country] = {'swim':0, 'bike':0, 'run':0} # Add each athlete's total to swim, bike, run for athlete in data: country = athlete['country'] # Note: some athletes who did not finish, so their times # are recorded as "DNF" = Did not finish or '---' if type(athlete['swim']) == str or type(athlete['bike']) == str or type(athlete['run']) == str: # skip this athlete pass else: results[country]['swim'] += athlete['swim'] results[country]['bike'] += athlete['bike'] results[country]['run'] += athlete['run'] # Divide totals by athlete count for each country for country in results: average = results[country]['swim'] / athletesByCountry[country] results[country]['swim'] = round(average, 2) average = results[country]['bike'] / athletesByCountry[country] results[country]['bike'] = round(average, 2) average = results[country]['run'] / athletesByCountry[country] results[country]['run'] = round(average, 2) return results if __name__ == '__main__': print('\n\n===== Task 5. buildDictWithAverageEventTimes =====') results = buildDictWithAverageEventTimes(kona) print(results) # {'NOR': {'swim': 0.95, 'bike': 5.08, 'run': 3.28}, # [...] # 'ESP': {'swim': 1.35, 'bike': 7.7, 'run': 4.55}} print(len(results)) # 22