【发布时间】:2013-06-03 22:48:47
【问题描述】:
所以我有一个代码
print
latOne = dL[1][3]
lonOne = dL [1][4]
x = [calculateDistance(latOne, lonOne, latTwo, lonTwo ) for latTwo, lonTwo in zip(latitude, longitude)]
print x
这会在输出列表中生成距离值
[0.0, 3043.004178666758, 21558.2996208357, 40246.748450913066, 40908.82213277263, 43786.0579097594, 67781.1426515405, 79693.11338661514, 65046.35819797423, 92804.01912347642]
现在每个距离都基于一个单独点的坐标(具有定义的名称) 即
Sydney (-20.7, 100)
Melbourne (-20, 120)
所以我的代码可以确定距离并生成距离列表。我想要做的是设置结构,以便距离与它们相关的点名称一起打印。即从悉尼取经纬度计算距离,然后输出为
Distance to Syndey is output ..., Distance to Melbourne is output, and so on.
如果您需要更多我的代码来帮助,请告诉我。
编辑:
这里是完整的脚本代码。
import math
import csv
def calculateDistance( latOne, lonOne, latTwo, lonTwo ):
from decimal import Decimal
latOne, lonOne, latTwo, lonTwo = [Decimal(x) for x in (latOne, lonOne, latTwo,lonTwo)]
DISTANCE_CONSTANT = 111120.0
coLat = math.fabs(lonOne - lonTwo)
alpha = 90 - latTwo
beta = 90 - latOne
cosAlpha = math.cos(math.radians(alpha))
cosBeta = math.cos(math.radians(beta))
sinAlpha = math.sin(math.radians(alpha))
sinBeta = math.sin(math.radians(beta))
cosC = math.cos(math.radians(coLat))
cos_of_angle_a = (cosAlpha * cosBeta)
cos_of_angle_b = (sinAlpha * sinBeta * cosC)
cos_of_angle_c = cos_of_angle_a + cos_of_angle_b
angle = math.degrees(math.acos(cos_of_angle_c))
distance = angle * DISTANCE_CONSTANT
return distance
stations = []
latitude = []
longitude = []
with open('data.csv', 'rU') as input:
dL= list(csv.reader(input))
sL = [row[4] for row in dL[1:]]
longitude.extend(sL)
sL1 = [row[3] for row in dL[1:]]
latitude.extend(sL1)
sL2 = [row[1] for row in dL[1:]]
stations.extend (sL2)
data = []
print "Station Coordinates"
for i in range(0, len(latitude)):
print str(stations[i]) + "(" + str(latitude[i]) + "," + str(longitude[i]) + ")"
ab = str(stations[i]) + "(" + str(latitude[i]) + "," + str(longitude[i]) + ")"
data.append(ab)
print stations
print
latOne = dL[1][3]
lonOne = dL [1][4]
x = [calculateDistance(latOne, lonOne, latTwo, lonTwo ) for latTwo, lonTwo in zip(latitude, longitude)]
print x
print
lessthan, greaterthan = [], []
knowndistance = float(raw_input("Please type your radius"))
for v in x:
if v <= knowndistance:
lessthan.append(v)
else:
greaterthan.append(v)
print lessthan
print greaterthan
【问题讨论】:
-
那么您到底需要什么帮助?不清楚...我经常处理这类事情,但我真的无法告诉您需要什么帮助...
-
你能举例说明 dL 的外观吗?它可以像循环遍历 dL 中的项目并将距离和相应的城市存储在字典中一样简单
-
@RyanSaxe 我想要帮助的是使用代码。这得到了距离,我想打印距离加上距离所属的车站名称。是不是更清楚了?
-
@looseeal_90 完整代码现已附上
-
我想我知道如何为您提供帮助,但如果您能展示几行 csv 的外观,那就太好了。你怎么知道正确的站的纬度和经度? (你打印 csv 可以回答我的第二个问题)
标签: python list parsing loops output