用python计算距离

2024-04-25 08:42:36 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中,我编写了一个程序,它接收如下数据:

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

将其解析为一个元组,每个元组中只有7、8、3、6个元素(按顺序排列)。所以它将解析:8266.99157657,453.7255798,Paved,1,American Legion,20,40.0188044212,-75.0547647126

一个元组,看起来像:('40.0188044212','75.0547647126','20','Paved')

前两个数字是Lat和Long值,我想用它们来计算它们和一组用户提供的点之间的距离,使用距离公式。你知道吗

到目前为止一切正常。我解析文件,把我需要的信息放到一个元组列表中,但是由于某种原因,我在循环中计算距离,对新列表排序,然后打印出来,打印出来的项目比我最初输入的要多,我真的很困惑为什么。我粘贴了下面的代码以及一个用户提供的点。你知道吗

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))

distanceList= []

for i in range(0,len(latLongList)):
    distance = findDistance(latLongList[i][0], latLongList[i][1], usrLocation[0], usrLocation[1])
    distanceList.append(distance)
    sorted_dist=sorted(distanceList, key=float)
    for i in range(0, (len(sorted_dist)-1)):
        print(sorted_dist[i])

用户点:[40.035580287799995,-75.1918309423]

输出:

0.0603546919869
0.0603546919869
0.0770100989575
0.0603546919869
0.0770100989575
0.0873576917421
0.0603546919869
0.0770100989575
0.0873576917421
0.0904729055369
0.00186470764073
0.0603546919869
0.0770100989575
0.0873576917421
0.0904729055369
0.00186470764073
0.0603546919869
0.0770100989575
0.0873576917421
0.0904729055369
0.090935558207
0.00186470764073
0.0603546919869
0.0770100989575
0.0873576917421
0.0904729055369
0.090935558207
0.105123571517

Tags: 用户test距离parkdistmathfloat元组
1条回答
网友
1楼 · 发布于 2024-04-25 08:42:36

计算每个项目的距离后,将打印到目前为止计算的所有项目。将内部for循环移到外部:

for i in range(0,len(latLongList)):
    distance = findDistance(latLongList[i][0], latLongList[i][1], usrLocation[0], usrLocation[1])
    distanceList.append(distance)
sorted_dist=sorted(distanceList, key=float)
for i in range(0, (len(sorted_dist)-1)):
    print(sorted_dist[i])

顺便说一下,您使用的索引变量可以直接在列表上循环,如下所示:

for latLong in latLongList:
    distance = findDistance(latLong[0], latLong[1], usrLocation[0], usrLocation[1])
    distanceList.append(distance)
sorted_dist=sorted(distanceList, key=float)
for dist in sorted_dist:
    print(dist)

我也不认为在sorted中使用key=float是必要的?你知道吗

相关问题 更多 >