gpt4 book ai didi

python - 计算 Python 中两个大列表的每个元素的距离

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:30 26 4
gpt4 key购买 nike

我有两个大的元组列表(大小 > 5000),其中包含这样的坐标:

l1 = [ (33.5779, -111.925),
(33.5738, -111.906),
(33.5556, -111.899),
...
(33.5646, -111.915),
(33.5733, -111.863)]

l2 = [ (33.4318, -111.938),
(33.5228, -111.9),
(33.5387, -111.885),
...
(33.5264, -111.925),
(33.538, -111.888)]

我想找到满足以下条件的组合:

distance <= arbitraryDistance

距离是:

 from geopy.distance import great_circle
great_circle((longitude1,latitude1), (longitude2,latitude2))

最快的方法是什么?

最佳答案

我找到的一个相对较快的解决方案是使用函数 cdist来自 scipy.spatial.distance

它计算两个列表中每个点组合之间的欧氏距离。尽管它不计算以米为单位的距离,但以度为单位,我仍然可以将我的米要求转换为度数。

所以我目前的解决方案是这样的:

from scipy.spatial.distance import cdist
l1 = [ (33.5779, -111.925),
(33.5738, -111.906),
(33.5556, -111.899),
...
(33.5646, -111.915),
(33.5733, -111.863)]

l2 = [ (33.4318, -111.938),
(33.5228, -111.9),
(33.5387, -111.885),
...
(33.5264, -111.925),
(33.538, -111.888)]

distanceRequired = 0.02 #arbitrary number in degrees

matrixOfDistances = cdist(l1,l2)
for index1,value1 in enumerate(matrixOfDistances):
isABusinessOpportunity = True
for index2,value2 in enumerate(value1):
if value2 <= distanceRequired:
isABusinessOpportunity = False
break

if isABusinessOpportunity:
print "The combination is:" + str(index1) + str(index2)

关于python - 计算 Python 中两个大列表的每个元素的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30981307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com