''' Purpose: determine a rough estimate of the geocenter of the continental USA ''' # get access to web-based data support import url # specify dataset web location CSV_WEB_FOLDER = 'http://www.cs.virginia.edu/~cs1112/datasets/csv/' # dataset name CONTINENTAL_DATASET = 'usa-continental.csv' # set link link = CSV_WEB_FOLDER + CONTINENTAL_DATASET # get the dataset of interest locations = url.get_dataset( link ) # determine geocenter - brainstorming # find the median latitude and median longitude # find the mean latitude and mean longitude # average min() and max() latitude and longitude # a loop that might be helpful for solving the problem latitudes = [] longitudes = [] # loop through the dataset to accumulate the columns of interest - latitude and longitude for location in locations : town, state, latitude, longitude = location # we can unpack the rows in this way latitudes.append( latitude ) longitudes.append( longitude ) # now we have a list of latitudes and a list of longitudes # mean latitude and longitude method: total_latitude = sum( latitudes ) total_longitude = sum( longitudes ) n = len( locations ) # number of locations is the length of our dataset - number of rows avg_latitude = total_latitude / n avg_longitude = total_longitude / n print( "Average latitude:", avg_latitude ) print( "Average longitude:", avg_longitude ) # these latitudes and longitudes likely do not actually show up in our lists, they were calculated print() # median method sorted_latitude = sorted( latitudes ) # sorted() is a built-in function that does not alter the input list sorted_longitudes = sorted( longitudes ) # and hands back a NEW sorted list median_location = n // 2 # needs to be an integer because it's an index median_latitude = sorted_latitude[ median_location ] median_longitude = sorted_longitudes[ median_location ] # these latitudes and longitudes do show up in our list, but there is no guarantee that they correspond to the same # location print( "Median latitude:", median_latitude ) print( "Median longitude:", median_longitude ) # look for location corresponding to our guess - we would need conditionals (if statements) # unlikely that the median latitude and median longitude correspond to the same location in the dataset