'''' DS2000 Fall 2024 Sample code from class - 10/18/24 Making improvements from tuesday's code: - do a better job splitting on commas (csv library) - write a function to return all the lats/longs as floats - write a function to count the # of dunks in a city ''' import csv import matplotlib.pyplot as plt FILENAME = "dunks.csv" LONG_POS = 0 LAT_POS = 1 CITY_POS = 2 def read_csv(filename): ''' parameters: string, name of a csv file returns: 2d list of strings does: uses the csv library to read in every row of csv file ''' data = [] with open(filename, "r") as infile: csvfile = csv.reader(infile, delimiter = ",") next(csvfile) for row in csvfile: data.append(row) return data def get_floats(lst, col): ''' parameters: 2d list of strings, int for column position returns: 1d list of floats does: iterates over 2d list, turns each value at given position into a float and adds onto a new list ''' floats = [] for row in lst: value = float(row[col]) floats.append(value) return floats def find_city(lst, city, city_pos): ''' parameters: 2d list, string for city, int for city_pos returns: int does: count number of times the given city exists in the list at the given position ''' count = 0 for row in lst: if city in row[city_pos]: count = count + 1 return count def main(): # step one: gather data by reading from csv file into a 2d list lst = read_csv(FILENAME) # prompt the user for a city city = input("What city to look for Dunks in?\n") count = find_city(lst, city, CITY_POS) print("There are", count, "dunks in", city) # call our new function to get a list of latitues, and a list of longitudes longs = get_floats(lst, LONG_POS) lats = get_floats(lst, LAT_POS) plt.scatter(longs, lats, color = "orange") plt.show() main()