您应该在解析de xml文件时存储元组(例如)所有点对及其距离的列表。
mypoints = [(distance12, x1, x2),...,(distancenm, xn, xm)]
mypoints.sort()
three_closer = mypoints[:3]
适应这代码:
..............
mypoints = []
for row in rows:
# Get coords for current record
curr_coords = row.getAttribute("lat") + ',' + row.getAttribute("lng")
# Get distance
tempDistance = distance.distance(user_coords, curr_coords).miles
mypoints.append((tempDistance, row))
mypoints.sort()
#the three closest points:
mythree_shorter = mypoints[0:3]
for distance, row in mythree_shorter:
shortestStation = json.dumps(
{'number': row.getAttribute("number"),
'address': row.getAttribute("address"),
'lat': row.getAttribute("lat"),
'lng': row.getAttribute("lng"),
'open': row.getAttribute("open")},
sort_keys=True,
indent=4)
save_in_some_way(shortestStation) #maybe writing to a file?
..................
感谢您的帮助!我不认为写入文件是可行的,因为这些数据将被iPhone使用。我不太理解第二个for()循环。它如何知道只能得到最近的3个?我假设它在'mypoints [0:3]'中完成,但我的python只是基本的。我会无论如何测试它,并让你知道。 – eoinzy 2011-04-22 22:41:38
谢谢华金!我使用了一些你的代码,并使其工作!唯一缺少的是“最短站点”上的“+ =”,所以我现在拥有的方式是“最短站点=最短站点+ json.dumps()....”。再次感谢!! – eoinzy 2011-04-22 22:55:11
最接近的三个存储在mythree_sorter中,并按循环顺序进行。第二个for循环是为了保存三个更接近的点,作为json字符串序列化(列表,文件),或者将它们发送到某处(取决于您)... – joaquin 2011-04-22 22:59:27