''' 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 = 'continental-usa.csv' # set link link = CSV_WEB_FOLDER + CONTINENTAL_DATASET # get the dataset of interest locations = url.get_dataset( link ) # get_dataset( link ) is another function in url # you give it a web link # returns a formatted dataset/table # so locations is a list of lists # each row is a location of a town, with 4 columns # to use a function in a module like url: module_name.function_name() # determine geocenter # a loop that might be helpful for solving the problem # for each location in locations dataset # strategy 1: find min and max latitude and longitude --> take average of each # so we need a list of latitudes and a list of longitudes # strategy 2: find average of latitudes and longitudes # also need a list of latitudes and longitudes latitudes = [] longitudes = [] for location in locations : zipcode, town, state, latitude, longitude = location # we know each location has 4 things in it, hence why we # could assign 4 variables at the same time # build up the 2 accumulator lists latitudes.append( latitude ) longitudes.append( longitude ) # print( latitudes ) # print( longitudes ) # strategy 1 min_lat = min( latitudes ) min_long = min( longitudes ) max_lat = max( latitudes ) max_long = max( longitudes ) min_max_lat_avg = ( min_lat + max_lat ) / 2 min_max_long_avg = ( min_long + max_long ) / 2 print( min_max_lat_avg, min_max_long_avg ) # strategy 2 avg_lat = sum( latitudes ) / len( latitudes ) avg_long = sum( longitudes ) / len( longitudes ) print( avg_lat, avg_long )