2013-10-19 214 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