Your code as it stands is quite confusing. I think what you are trying to achieve is:
start = (43.82846160000000000000, -79.53560419999997000000)
locations = {'one':(43.65162010000000000000, -79.73558579999997000000),
'two':(43.75846240000000000000, -79.22252100000003000000),
'thr':(43.71773540000000000000, -79.74897190000002000000)}
def dis(start, locations):
nodes = []
while locations:
# until the dictionary of locations is empty
nearest = min(locations, key=lambda k: calc_distance(start, locations[k]))
# find the key of the closest location to start
nodes.append((start, locations[nearest]))
# add a tuple (start, closest location) to the node list
start = locations.pop(nearest)
# remove the closest location from locations and assign to start
return nodes
def calc_distance(start, destination):
# ...
nodes = dis(start, locations)
Note that I have made start
an explicit argument to calc_distance
, and start
and locations
explicit arguments to dis
- wherever possible, don't rely on scope for access to variables. The output I get in nodes
is:
[((43.8284616, -79.53560419999997), (43.7177354, -79.74897190000002)),
((43.7177354, -79.74897190000002), (43.6516201, -79.73558579999997)),
((43.6516201, -79.73558579999997), (43.7584624, -79.22252100000003))]