I have a file of test data that looks like such:
GIS_FPC_PP,PERIMETER,MAT,LIGHTS,PARK,SPACES,LAT,LNG
8266.99157657,453.7255798,Paved,1,American Legion,20,40.0188044212,-75.0547647126
20054.5870679,928.20201772,Paved,1,Barnes Foundation Museum, ,39.9610355788,-75.1725011285
9474.06791225,475.46041689,Paved,1,Carousel House, ,39.9788367755,-75.2123945669
500.21243962,146.87828141,Paved,1,Center Square,3,39.9531308619,-75.1629612614
9109.54965748,494.92895804,Paved,1,Clarence H Clark Park,33,39.9467944475,-75.2092212039
1118.07293627,159.7527515,Paved,0,Clarence H Clark Park,5,39.94626513,-75.2089212417
749.52528516,167.7006179,Paved,0,Clifford Park - Thomas Mansion,4,40.0349216312,-75.1900864349
2386.07209112,208.90531203,Grass,0,Cobbs Creek South, ,39.9373184367,-75.2341880089
8367.54199083,407.57978846, ,0,Cobbs Creek South, ,39.9413269464,-75.2383849209
1145.41060379,155.30785641,Grass,0,Cobbs Creek South,6,39.9529398119,-75.2522393913
I am writing a program that takes in that data, parses it, and extracts the needed data (LAT, LNG, SPACES, MAT) into a list of tuples. It then calculates the distance between each latitude and longitude point using the distance formula. Finally, it puts each tuple into a dictionary with the distance value as the key, sorts it, and prints it. I got everything to work, but the sorting isn't working correctly which really confuses me. I have pasted the program below along with the user location I used to produce the output. Any help would be greatly appreciated.
Program:
import urllib, philly_loc,math
def findDistance(pLat,pLong,uLat,uLong):
dist=math.sqrt(math.pow((float(pLat)-float(uLat)),2)+math.pow((float(pLong)-float(uLong)),2))
return dist
test=open("testdata.txt")
parkingDataList=test.readlines()
test.close()
usrLocation=philly_loc.getLoc()
latLongList=[]
for i in range(0, len(parkingDataList)):
entry=parkingDataList[i]
tList=[]
if i!=0:
parseCSV=entry.split(',')
tList.append(parseCSV[-2].strip())
tList.append(parseCSV[-1].strip())
tList.append(parseCSV[-3].strip())
tList.append(parseCSV[-6].strip())
latLongList.append(tuple(tList))
latLongDict=dict()
print usrLocation
for i in range(0,len(latLongList)):
distance = findDistance(latLongList[i][0], latLongList[i][1], usrLocation[0], usrLocation[1])
latLongDict.update({distance: latLongList[i]})
sorted(latLongDict, key=latLongDict.get)
for keys, values in latLongDict.items():
print(keys)
print(values)
Output:
[40.0405802878, -75.2238309423]
0.0946684240633
('39.9610355788', '-75.1725011285', '', 'Paved')
0.0627937257489
('39.9788367755', '-75.2123945669', '', 'Paved')
0.0946684240633
('39.9610355788', '-75.1725011285', '', 'Paved')
0.0627937257489
('39.9788367755', '-75.2123945669', '', 'Paved')
0.106548205724
('39.9531308619', '-75.1629612614', '3', 'Paved')
0.0946684240633
('39.9610355788', '-75.1725011285', '', 'Paved')
0.0627937257489
('39.9788367755', '-75.2123945669', '', 'Paved')
0.106548205724
('39.9531308619', '-75.1629612614', '3', 'Paved')
0.0946684240633
('39.9610355788', '-75.1725011285', '', 'Paved')
0.0949169547388
('39.9467944475', '-75.2092212039', '33', 'Paved')
0.0627937257489
('39.9788367755', '-75.2123945669', '', 'Paved')
0.0954863768441
('39.94626513', '-75.2089212417', '5', 'Paved')
0.106548205724
('39.9531308619', '-75.1629612614', '3', 'Paved')
0.0946684240633
('39.9610355788', '-75.1725011285', '', 'Paved')
0.0949169547388
('39.9467944475', '-75.2092212039', '33', 'Paved')
0.0949169547388
('39.9467944475', '-75.2092212039', '33', 'Paved')
0.0946684240633
('39.9610355788', '-75.1725011285', '', 'Paved')
0.0954863768441
('39.94626513', '-75.2089212417', '5', 'Paved')
0.0627937257489
('39.9788367755', '-75.2123945669', '', 'Paved')
0.106548205724
('39.9531308619', '-75.1629612614', '3', 'Paved')
0.0342156714706
('40.0349216312', '-75.1900864349', '4', 'Paved')
0.0949169547388
('39.9467944475', '-75.2092212039', '33', 'Paved')
0.0946684240633
('39.9610355788', '-75.1725011285', '', 'Paved')
0.103779953368
('39.9373184367', '-75.2341880089', '', 'Grass')
0.0954863768441
('39.94626513', '-75.2089212417', '5', 'Paved')
0.0627937257489
('39.9788367755', '-75.2123945669', '', 'Paved')
0.106548205724
('39.9531308619', '-75.1629612614', '3', 'Paved')
0.0342156714706
('40.0349216312', '-75.1900864349', '4', 'Paved')
0.0949169547388
('39.9467944475', '-75.2092212039', '33', 'Paved')
0.0946684240633
('39.9610355788', '-75.1725011285', '', 'Paved')
0.103779953368
('39.9373184367', '-75.2341880089', '', 'Grass')
0.0954863768441
('39.94626513', '-75.2089212417', '5', 'Paved')
0.0627937257489
('39.9788367755', '-75.2123945669', '', 'Paved')
0.106548205724
('39.9531308619', '-75.1629612614', '3', 'Paved')
0.100314725101
('39.9413269464', '-75.2383849209', '', '')
0.0342156714706
('40.0349216312', '-75.1900864349', '4', 'Paved')
usrLocation: [40.0405802878, -75.2238309423]