首页 > 解决方案 > 优化对称测试

问题描述

我有 n 个项目。对于每一对,我必须运行一个测试(真/假)。测试是对称的:test(a,b)=test(b,a)。我正在寻找运行这些测试的最快解决方案。

每个项目都有一个 id 和一个属性。在这种特殊情况下,该属性是一个多边形,我正在寻找其他附近的多边形。这个测试是对称的:如果 A 接触 B,则 B 接触 A。测试两次是多余的。所以我写了这段代码只测试 n(n-1)/2 次:

from collections import defaultdict
dict_neigh = defaultdict(list)

# initially my data is in a dataframe, but I though an array would be faster(?) 

# the property of the item:
data=df_gps.coordinates.values

# the index of the item:
ind=df_gps.index.values

end=len(data)
for x in range(0,end-1):
    for y in range(x +1,end):
        # The symmetric test:
        if Polygon(data[x]).touches(Polygon(data[y])):
            # Store the result in a dic
            dict_neigh[ind[x]].append(ind[y])
            dict_neigh[ind[y]].append(ind[x])

我想知道是否有人找到提高这种(某种)测试速度的方法。

标签: python-3.xperformancefor-loopcomputational-geometry

解决方案


推荐阅读