我正在做一个 python
在项目中,我从一个函数中获取坐标值。x, y
在...中 dict
像下面这样。
centroid_dict = {0: (333, 125), 1: (288, 52), 2: (351, 41)}
其中 0, 1, 2
是 objectId
和 (333, 125), (288, 52), (351, 41)
是他们 (x, y)
我需要计算每个坐标之间的距离。 我需要计算每个坐标之间的距离,这意味着。
0 - 1 -> ((333, 125) - (288, 52))
0 - 2 -> ((333, 125) - (351, 41))
1 - 0 -> ((288, 52) - (333, 125))
1 - 2 -> ((288, 52) - (351, 41))
2 - 0 -> ((351, 41) - (333, 125))
2 - 1 -> ((351, 41) - (288, 52))
要计算距离,我可以用..:
def calculateDistance(x1, y1, x2, y2):
dist = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return dist
但我想不出有什么逻辑可以计算出各点之间的距离,因为将来dict的长度可能会增加。到目前为止,它是 3
但它可以 10
. 谁能帮我给一些想法。谅谅
你可以使用itertools中的组合来组成一个新的字典,以每一对对象作为key。
from itertools import combinations:
distances = dict()
for (id1,p1),(id2,p2) in combinations(centroid_dict.items(),2):
dx,dy = p1[0]-p2[0], p1[1]-p2[1]
distances[id1,id2] = distances[id2,id1] = math.sqrt(dx*dx+dy*dy)
这种方法的缺点是,它将系统地计算所有的距离,而你的程序可能不需要访问所有这些值。 一个更有效的方法是使用字典作为距离的缓存,并使用一个函数 "按需 "获取它们。
distanceCache = dict()
def getDist(id1,id2):
if id1 > id2: return getDist(id2,id1) # only store each pair once
if (id1,id1) in distanceCache: return distanceCache[id1,id2]
x1,y1 = centroid_dict[id1]
x2,y2 = centroid_dict[id2]
dx,dy = x1-x2,y1-y2
return distanceCache.setDefault((id1,id2),math.sqrt(dx*dx+dy*dy))
这将允许你在对象位置改变时简单地清除缓存,而不会产生O(n^2)时间的即时延迟。
注意,你也可以使用点(位置)本身作为缓存的键,也可以使用LRU缓存(来自functools)。
from functools import lru_cache
import math
@lru_cache(1024)
def getDist(p1,p2):
dx,dy = p1[0]-p2[0],p1[1]-p2[1]
return math.sqrt(dx*dx+dy*dy)
def getObjectDist(id1,id2):
return getDist(centroid_dict[id1],centroid_dict[id2])
找出四个二维坐标之间的欧几里得距离。
from scipy.spatial import distance
coords = [(35.0456, -85.2672),
(35.1174, -89.9711),
(35.9728, -83.9422),
(36.1667, -86.7833)]
distance.cdist(coords, coords, 'euclidean')
array([[ 0. , 4.7044, 1.6172, 1.8856],
[ 4.7044, 0. , 6.0893, 3.3561],
[ 1.6172, 6.0893, 0. , 2.8477],
[ 1.8856, 3.3561, 2.8477, 0. ]])
你可以这样做 不需要导入。
def dist(key1,key2):
return calculateDistance(*centroid_dict[key1],*centroid_dict[key2])
all_dist = []
for key1 in centroid_dict:
all_dist.extend([dist(key1,key2) for key2 in centroid_dict if not key1==key2])