Distance formula between two points in a list

前端 未结 7 1734
刺人心
刺人心 2021-01-31 19:39

I need to take a list I have created and find the closest two points and print them out. How can I go about comparing each point in the list?

There isn\'t any need to pl

7条回答
  •  醉话见心
    2021-01-31 19:51

    First, some notes:

    a**2 # squares a
    (xi - xii)**2 # squares the expression in parentheses.
    

    mInput doesn't need to be declared in advance.
    fList.append((x, y)) is more pythonic than using +=.

    Now you have fList. Your distance function can be rewritten to take 2 2-tuple (point) arguments, which I won't bother with here.

    Then you can just write:

    shortest = float('inf')
    for pair in itertools.combinations(fList, 2):
        shortest = min(shortest, distance(*pair))
    

提交回复
热议问题