对于大型代码转储感到抱歉。我的问题是我的最后一个循环。我正在尝试建立一个新词典,"候选人,"包含一个站点与另一个站点的所有可能组合,映射到它们之间的距离。例如,假设站点1的ID为1234,站点2的ID为1235,站点3的ID为1236.我希望候选词典最终是{' 1234_1235' :距离,' 1234_1236' :distance},即从一个站点到其他站点的所有可能组合。这些组合已包含在dictkey中;我只需要重组它们。这样我就可以弹出最短的距离,并最终得到一个最近邻居的列表"到每个网站。
for i in np.arange(num_sites):
lat1 = lat[i]
lon1 = lon[i]
site1=site[i]
rat1 = lat1*np.pi/180.0
ron1 = lon1*np.pi/180.0
for j in np.arange(i+1,num_sites):
lat2 = lat[j]
lon2 = lon[j]
site2= site[j]
rat2 = lat2*np.pi/180.0
ron2 = lon2*np.pi/180.0
d = 2.0*np.arcsin(np.sqrt((np.sin((rat1-rat2)/2))**2 +
np.cos(rat1)*np.cos(rat2)*(np.sin((ron1-ron2)/2))**2))
# dist_arr[i,j] = 6371.0 * d
dictkey[site1+"_"+site2] = 6371.0*d
temporary = set()
for key in dictkey:
parts = key.split("_")
site_one = parts[0]
site_two = parts[1]
temporary.add(site_one)
for temps in temporary:
candidates = dict()
for key in dictkey:
parts = key.split("_")
site_one = parts[0]
site_two = parts[1]
if site_one == temps:
candidates[site_one] = dictkey[key]
答案 0 :(得分:0)
实现它的最简单方法是在计算距离时准备候选人。只准备候选人的词典而不是dict_key:
candidates = {}
for i in np.arange(num_sites):
lat1 = lat[i]
lon1 = lon[i]
site1=site[i]
rat1 = lat1*np.pi/180.0
ron1 = lon1*np.pi/180.0
for j in np.arange(i+1,num_sites):
lat2 = lat[j]
lon2 = lon[j]
site2= site[j]
rat2 = lat2*np.pi/180.0
ron2 = lon2*np.pi/180.0
d = 2.0*np.arcsin(np.sqrt((np.sin((rat1-rat2)/2))**2 +
np.cos(rat1)*np.cos(rat2)*(np.sin((ron1-ron2)/2))**2))
distance = 6371.0*d
if site1 in candidates:
candidates[site1][site2] = distance
else:
candidates[site1] = {site2: distance}
if site2 in candidates:
candidates[site2][site1] = distance
else:
candidates[site2] = {site1: distance}
上面的解决方案会为您提供字典候选词,其中的词典与所有其他站点保持距离。它当然是双倍大小 - 它为1234->存储相同的数据。 4567和4567 - > 1234。
candidates.get('1234', {}).get('4567') == candidates.get('4567', {}).get('1234')
>>True
但是如你所见,它提供了获取价值的简便方法。 如果你需要得到例如最低距离到1234的网站:
import operator
closest_site = sorted(candidates.get('1234', {}).items(), key=operator.itemgetter(1))
取自回答1
nearest_site将是元组:(<ID of closest site>, <distance>)
如果您需要更高效的内存解决方案,请写信。这不是最佳的,因为生成双倍大小的数据。
<强>更新强>
如果您需要获取原始网站的ID以及最短距离网站:
closest_site = (candidates.get('1234'),) + sorted(candidates.get('1234', {}).items(), key=operator.itemgetter(1))
then_mostsite:(<ID of original site>, <ID of closest site>, <distance>)