Distance formula between two points in a list

前端 未结 7 1756
刺人心
刺人心 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:58

    I realize that there are library constraints on this question, but for completeness if you have N points in an Nx2 numpy ndarray (2D system):

    from scipy.spatial.distance import pdist
    x = numpy.array([[9.5,7.5],[10.2,19.1],[9.7,10.2]])
    mindist = numpy.min(pdist(x))
    

    I always try to encourage people to use numpy/scipy if they are dealing with data that is best stored in a numerical array and it's good to know that the tools are out there for future reference.

提交回复
热议问题