Given a set of arrays within an array how do I find the arrays with the minimum values based on two elements/columns in the array? Those two elements/columns are the destination zip code and distance.
I have an array of arrays that have a origin zip code, origin latitude, origin longitude, destination zip code, destination latitude, destination longitude, and miles between the two points. I need to keep only those combinations that represent the minimum mileage between to the destination zip code. For example a point in New Jersey may have a distance from the Philadelphia Office that is 45 miles, from the Newark Office that is 78 miles and one from the Delaware Office that is 58 miles. I need to keep the mileage from the Philadelphia Office that is 45 miles and produce a .csv file that has origin zip code, origin latitude, origin longitude, destination zip code, destination latitude, destination longitude, and miles between the two points. The array looks like this: [['37015', 'TN31', 36.2777, -87.0046, 'NY', 'White Plains', '10629', 41.119008, -73.732996, 77.338920003], ['72202', 'ARB1', 34.739224, -92.27765, 'NY', 'White Plains', '10629', 41.119008, -73.732996, 1099.7837975322097]] My code looks like this : import csv import math def calculate_distance(lat1, lon1, lat2, lon2): if (not lat1) or (not lon1) or (not lat2) or (not lon2): return -1 lat1 = float(lat1) * math.pi/180 lon1 = float(lon1) * math.pi/180 lat2 = float(lat2) * math.pi/180 lon2 = float(lon2) * math.pi/180 return 3959.0 * math.acos(math.sin(lat1) * math.sin(lat2) + math.cos(lat1) * math.cos(lat2) * math.cos(lon2-lon1)) #Above function changed from the following URL: http://iamtgc.com/geocoding- with-python/ InputPath = "C:\\Users\\jacobs\\Downloads\\ZipCodes\\" ZipCodes = "zipcode.csv" RptgOfficeFile = "Reporting_Office_2015072001.csv" InputFile = InputPath+RptgOfficeFile zInputFile = InputPath+ZipCodes zOutputFile = InputPath+'Zip_Code_Distance.csv' z1OutputFile = InputPath+'Minimum_Distance_Zip_Code_File.csv' f = open(InputFile, 'r') zO = open(zOutputFile,'w') z1 = open(z1OutputFile,'w') lines = [ ] OfficeZipcodes = [] ZipRptOffice = {} OLatitude = [ ] OLongitude = [ ] OLocationCode = [] dzip = [] dLatitude = [] dLongitude = [] dCity = [] dState = [] Combined =[] Answers = [] for line in f: l = [i.strip() for i in line.split(',')] OfficeZipcodes.append(l[4]) ZipRptOffice[l[4]]= l[3] OLatitude.append(l[5]) OLongitude.append(l[6]) OLocationCode.append(l[3]) del OfficeZipcodes[0] del OLatitude[0] del OLongitude[0] del OLocationCode[0] zf = csv.DictReader(open(zInputFile)) #http://courses.cs.washington.edu/courses/cse140/13wi/csv-parsing.html for row in zf: dzip.append(row["zip"]) dLatitude.append(float(row["latitude"])) dLongitude.append(float(row["longitude"])) dCity.append(row["city"]) dState.append(row["state"]) for i in range(len(OfficeZipcodes)): for j in range(len(dzip)): Distance = calculate_distance(OLatitude[i], OLongitude[i],dLatitude[j],dLongitude[j]) Combined.append([OfficeZipcodes[i], OLocationCode[i],float(OLatitude[i]),float(OLongitude[i]),dState[j],dCity[j],dzip[j], dLatitude[j],dLongitude[j],Distance]) for i in range(len(Combined)): zO.write(str(Combined[i][0])+","+str(Combined[i][1])+","+str(Combined[i][2])+","+ str(Combined[i][3])+","+str(Combined[i][4])+","+ str(Combined[i][5])+","+ str(Combined[i][6])+","+str(Combined[i][7])+","+ str(Combined[i][8])+","+str(Combined[i][9])+"\n") zO.close() f.close() I am using Python 2.7 on a Windows 7 machine. Please help me get my head around how to accomplish this task. Thank you very much. Robert Davis -- https://mail.python.org/mailman/listinfo/python-list