so i have a list of points
["9.5 7.5", "10.2 19.1", "9.7 10.2", "2.5 3.6", "5.5 6.5", "7.8 9.8"]
with a starting point of
["2.2 4.6"]
now what i am trying to do it is get the closest point to my starting point, then the closest point to that point and so on.
So i get to calculate distance
def dist(p1,p2):
return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
but again, i'm trying to get the closest to my starting point, then the closest point to that one and so on.
ok, because you are complaing i didn't show enough code?
fList = ["2.5 3.6", "9.5 7.5", "10.2 19.1", "9.7 10.2", "5.5 6.5", "7.8 9.8"]
def distance(points):
p0, p1 = points
return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)
min_pair = min(itertools.combinations(fList, 2), key=distance)
min_distance = distance(min_pair)
print min_pair
print min_distance
so i get passing my starting point I get
([2.2, 4.6], [2.5, 3.6])
So now i need to use 2.5, 3.6 as my starting point and find the next closest and so on
Has anyone done anything similar?
sort()
on the list of floatpoints if you give sort() askey=
your distance function. no need for itertools.