python-3.x - 优化对称测试
问题描述
我有 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])
我想知道是否有人找到提高这种(某种)测试速度的方法。
解决方案
推荐阅读
- jenkins - 如何将 Jenkinsfile 拆分为子文件(“包含”)?
- javascript - 在将鼠标悬停在工具提示框上的断开条值上 react-chartjs-2/chartjs
- android - 访问被拒绝查找属性 ro.vendor.hwui.texture_cache_size
- python - 将多个列表合并为单个列表格式
- javascript - 从 Angular 中的 @Input 更新 ngOnChanges 视图
- javascript - CKFinder - 图像切割到指定尺寸
- amazon-web-services - SonicWall 和 AWS 之间的站点到站点连接 - IAM 策略
- mongodb - 汇总聚合中的所有命名字段
- sql-server-2016 - 为什么 SQL Server 不能正确显示 sys.columns.object_id?
- r - 使用 ggplot2 绘制条形图不起作用