参考计算列表的部分(Python)

时间:2013-10-19 04:55:04

标签: python

我必须计算给定的一组点与来自列表的多个点之间的距离。

列表中一行的示例是;

['14', '"Name of place"', '-31.000', '115.000']

由于calc距离函数有四个参数,我把两个给定的点放在那里然后是列表的long和lat值。

我的理解是这样做我可以简单地参考名单'List'然后我要访问的每行的哪一部分又名2和3

        User_E = raw_input("First enter your longitude(easting) value")
        User_N = raw_input("Now enter your latitude(northing) value")
        Radius = raw_input("Now enter a search radius in kilometres")
        for lines in ListOfLandmarks:
            CalculateDistance( User_N, User_E, ListOfLandmarks[2], ListOfLandmarks[3] )

当我运行程序时,我收到以下错误:

TypeError: unsupported operand type(s) for -: 'str' and 'list'

我试图使用intfloat将它们标识为数字,但它们会产生以下结果:

TypeError: int() argument must be a string or a number, not 'list'

TypeError: float() argument must be a string or a number

def CalculateDistance( 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

只是想知道我哪里出错了,欢呼!

1 个答案:

答案 0 :(得分:2)

跳过转换问题(将坐标存储为字符串而不是浮点数)

for lines in ListOfLandmarks:
        CalculateDistance( User_N, User_E, ListOfLandmarks[2], ListOfLandmarks[3] )

应该是

for lines in ListOfLandmarks:
        CalculateDistance( User_N, User_E, lines[2], lines[3] )

当您要求与您迭代的特定地标的距离时,ListOfLandmarks[2]第二个地标(所以list您的译员不知道如何比较/在float上下文中使用),而当前地标的第一个坐标lines[2]