0
我有下面的代碼,它將使用谷歌地圖API計算任意兩點之間的距離。計算從一個點到列表中每個其他點的距離
代碼:
def calculateDistance(sourceLatitude, sourceLongitude, destinationLatitude, destinationLongitude):
sourceCoordinates = sourceLatitude, sourceLongitude
destinationCoordinates = destinationLatitude, destinationLongitude
url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=en-EN&sensor=false".format(str(sourceCoordinates),str(destinationCoordinates))
result= simplejson.load(urllib.urlopen(url))
resulting_distance = result['rows'][0]['elements'][0]['distance']['text']
print "The distance is: " + resulting_distance
我有一個TSV文件,如下圖所示:
Longitude Latitude
-4.143452 5.342044
-3.913602 5.341612
-3.967045 5.263331
... ...
... ...
我有以下方法,讀取TSV
def readTSV():
with open('TSVFile.txt','r') as f:
myList = zip(*(line.strip().split('\t') for line in f))
現在,清單「 myList「,將tsv文件中的所有值包含到列表中。
我的問題是,我如何將這些值列表傳遞給「calculateDistance」方法,以便列表中值的每個組合都被覆蓋?更確切地說,我需要計算從一個點到列表中每個其他點的距離。
你可以看看['itertools.combinations'(https://開頭docs.python.org/2/library/itertools.html#itertools.combinations)。 – jonrsharpe 2014-10-16 14:31:24
爲什麼你不通讀文檔,並嘗試在解釋器中的實現?這不是一個代碼寫入服務。 – jonrsharpe 2014-10-16 14:33:41
@jonrsharpe,肯定感謝您的意見 – Varun 2014-10-16 14:35:26