python计算最近xy点的距离

python计算最近xy点的距离,python,Python,所以我有一个要点列表 ["9.5 7.5", "10.2 19.1", "9.7 10.2", "2.5 3.6", "5.5 6.5", "7.8 9.8"] 以 ["2.2 4.6"] 现在我要做的是得到离起点最近的点,然后是离起点最近的点,依此类推 所以我开始计算距离 def dist(p1,p2): return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2) 不过,我还是想尽量接近我的起点,然后是最接近那个起点

所以我有一个要点列表

["9.5 7.5", "10.2 19.1", "9.7 10.2", "2.5 3.6", "5.5 6.5", "7.8 9.8"]

["2.2 4.6"]
现在我要做的是得到离起点最近的点,然后是离起点最近的点,依此类推

所以我开始计算距离

def dist(p1,p2):
    return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
不过,我还是想尽量接近我的起点,然后是最接近那个起点的点,以此类推

好吧,因为你抱怨我没有显示足够的代码

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
所以我通过了我的起点

([2.2, 4.6], [2.5, 3.6])
所以现在我需要使用2.5,3.6作为我的起点,并找到下一个最接近的,以此类推


有人做过类似的事情吗?

一种可能性是使用广度优先搜索来扫描所有元素,并为从队列中弹出的每个元素找到最近的点:

import re, collections
import math

s = ["9.5 7.5", "10.2 19.1", "9.7 10.2", "2.5 3.6", "5.5 6.5", "7.8 9.8"]
def cast_data(f):
   def wrapper(*args, **kwargs):
     data, [start] = args
     return list(map(lambda x:' '.join(map(str, x)), f(list(map(lambda x:list(map(float, re.findall('[\d\.]+', x))), data)), list(map(float, re.findall('[\d\.]+', start))))))
   return wrapper

@cast_data
def bfs(data, start, results=[]):
   queue = collections.deque([start])
   while queue and data:
     result = queue.popleft()
     possible = min(data, key=lambda x:math.hypot(*[c-d for c, d in zip(result, x)]))
     if possible not in results:
       results.append(possible)
       queue.append(possible)
       data = list(filter(lambda x:x != possible, data))
   return results

print(bfs(s, ["2.2 4.6"]))
输出:

['2.5 3.6', '5.5 6.5', '7.8 9.8', '9.7 10.2', '9.5 7.5', '10.2 19.1']
 [[2.5, 3.6], [5.5, 6.5], [7.8, 9.8], [9.5, 7.5], [9.7, 10.2], [10.2, 19.1]]

[(1.0440306508910546, [2.5, 3.6]), (3.8078865529319543, [5.5, 6.5]),
 (7.64198926981712, [7.8, 9.8]), (7.854934754662192, [9.5, 7.5]), 
 (9.360021367496977, [9.7, 10.2]), (16.560495161679196, [10.2, 19.1])]

结果是使用
math.hypot
确定的最近点列表。您可以尝试以下代码。简单得多,简短得多。使用比较器根据与起点的距离对列表进行排序
(2.2,4.6)

您可以简单地根据您的距离函数定义-f.e.:

import math

def splitFloat(x):
    """Split each element of x on space and convert into float-sublists"""
    return list(map(float,x.split()))

def dist(p1, p2):
    # you could remove the sqrt for computation benefits, its a symetric func
    # that does not change the relative ordering of distances
    return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)

p = ["9.5 7.5", "10.2 19.1", "9.7 10.2", "2.5 3.6", "5.5 6.5", "7.8 9.8"]

s = splitFloat("2.2 4.6")       # your start point
p = [splitFloat(x) for x in p]  # your list of points

# sort by distance between each individual x and s
p.sort(key = lambda x:dist(x,s))

d = [ (dist(x,s),x) for x in p]  # create tuples with distance for funsies
print(p)
print(d)
输出:

['2.5 3.6', '5.5 6.5', '7.8 9.8', '9.7 10.2', '9.5 7.5', '10.2 19.1']
 [[2.5, 3.6], [5.5, 6.5], [7.8, 9.8], [9.5, 7.5], [9.7, 10.2], [10.2, 19.1]]

[(1.0440306508910546, [2.5, 3.6]), (3.8078865529319543, [5.5, 6.5]),
 (7.64198926981712, [7.8, 9.8]), (7.854934754662192, [9.5, 7.5]), 
 (9.360021367496977, [9.7, 10.2]), (16.560495161679196, [10.2, 19.1])]

欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。在这里申请。StackOverflow不是设计、编码、研究或教程服务。编辑得不错,你想得太多了。如果将sort()作为距离函数指定为
key=
,则可以通过在浮点列表上使用
sort()
轻松解决此问题。你是邪恶的:)-我得到了
[(1.0440306508910546,[2.5,3.6]),(3.8078865529319543,[5.5,6.5]),(7.64198926981712,[7.8,9.8]),(7.854934754662192,[9.5,7.5]),(9.360021367496977,[9.7,10.2],(16.5604951679196,[10.2,19.1])
-但如果我发布我的6行程序,那将是一个复制、粘贴和理解solution@PatrickArtner啊,你在每一点上都包括了差异。