我有一个列表,我想计算列表中每个项目与列表中所有其他项目的平均距离

问题描述 投票:0回答:2

列表包含x,y坐标,看起来像这样,[[1,2], [2,3], [5,6]....]我知道如何计算两个坐标之间的距离。我需要计算[1,2]与列表中所有其他坐标之间的距离,然后移至[2,3]并执行相同操作,依此类推。

解决此问题的最佳方法是什么?

我最初的方法是创建两个for循环:

for i in range (0, len(coordinateslist)):
    for j in range (0, len(coordinateslist)):
        distanceList.append(computeDist(coordinateslist[i),coordinateslist[j])
python list distance
2个回答
1
投票

您需要定义要比较的坐标对。请参阅下表以了解可能存在的比较。

*   A  B  C  ...

A   AA AB AC
B   BA BB BC
C   CA CB CC
...          ...

假设有效的比较是(AB,AC,BC)或(BA,CA,CB),但不是两者。

您需要稍微更改循环。

from itertools import islice

for i, point in enumerate(coordinateslist):
    for other in islice(coordinateslist, i):
        distanceList.append(computeDist(point, other))

0
投票

所以蛮力解决方案可能看起来像...ex [x,y,z,l,m ...]一次计算每个配对距离x :(点-x)y :(点-x -y)z :(点-x -y -z)等等...

def calculate_distances(points)
    tocalc = points
    answers = dict()
    for point in points:
        for dot in tocalc:
            if point!=dot: # distance to itself is always 0
                answers[sorted([point,dot])] = distance(point,dot)
        tocalc.pop(0) #no need to process this item again
     return answers

然后您可以执行sum(answers.values()),'sorted(answers,key = lambda k:k.value)'等操作

从上面很明显,我们实际上不需要第二个列表来管理要计算的内容,我们只需要两个索引,所以让我们尝试以最小的内存占用量来做到这一点:

def calculate_distances(points):
    currind=0
    tocalc_ind = 1
    # we also know the answer looks like a matrix with diagonal of zeros...
    answers = dict()
    for p_ind in range(len(points)):
        currind = p_ind
        if points[currind] not in answers:
            answers[points[currind]] = dict()
        for c_ind in range(tocalc_ind,len(points)): # implicitly skipping previous
            answers[points[currind]][points[c_ind]] = distance(points[currind],points[c_ind])
    return answers

注意,我更改了数据格式以帮助可视化答案。我确定还有其他优化方法,但这应该在O(n)时间内运行,因为通常O(n * n)的第二个嵌套循环每转都会减少一次。

© www.soinside.com 2019 - 2024. All rights reserved.