2013-10-19 150 views
0

我必须计算一组给定的点与多个来自列表的点之间的距离。参考计算列表的部分(Python)

从列表中的一行的例子是;

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

随着计算距离函数有四个参数,我把两个给分的名单,然后再长和纬度值。

我的理解是要做到这一点,我可以简单地指到列表中又名“列表”,然后每一行我想访问又名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' 

Iv'e试图用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 

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

+0

'Listname [2]'和3是一个字符串,因此当您调用该函数时将它们包装在'float()'中...什么是'givena'和'givenb '?我们需要更多的实际代码来看看发生了什么。 – beroe

+0

Iv'e试图 CalculateDistance(浮动(givena),浮子(givenb),浮法(LISTNAME [2]),浮动(LISTNAME [3])) 它获取 类型错误:浮子()参数必须是一个字符串或一个数字 对不起,给出a和b是用户输入坐标 – user2896995

+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]

+0

工作,欢呼! – user2896995

+0

@ user2896995请考虑接受此作为答案,如果它可以帮助你。 http://meta.stackexchange.com/a/5235/235416 – thefourtheye