如何在另一个列表中找到距离 (x,y) 位置最近的 (x, y) 位置? [英] How to find the closest (x, y) position to (x,y) position in another list?
问题描述
我有两个带有 x 和 y 坐标的 2D 列表,我想通过 list1,并为每个点在 list2 中找到最近的 (x, y) 坐标.它们的长度不同,如果我不使用list2的所有点,或者即使我重复使用点也没关系,只要我在list1中只遍历所有点一次.我需要班次本身以及两个点列表中的位置.这是我为找到转变所做的工作:
I have two 2D lists with x and y coordinates and I want to go through list1, and for each point find the closest (x, y) coordinates in list2. They're of different lengths, and it's okay if I don't use all the points of list2 or even if I reuse points, as long as I go through all the points only once in list1. I need the shift itself as well as the location in the lists of both points. Here's what I have done to find the shift:
s_x = ([1.0,2.0,3.0])
s_y = ([1.5,2.5,3.5])
SDSS_x = ([3.0,4.0,5.0])
SDSS_y = ([3.5,4.5,5.5])
list1 = zip(s_x,s_y)
list2 = zip(SDSS_x,SDSS_y)
shift = []
place_in_array = []
for num,val in enumerate(list1):
guess = 9999999999999.0
place_guess = 0
for index,line in enumerate(list2):
new_guess = math.hypot(line[0] - val[0], line[1] - val[1])
if new_guess < guess:
guess = new_guess
place_guess = index
shift.append(guess)
place_in_array.append(place_guess)
print shift
print place_in_array
但输出是这样的:
[2.8284271247461903, 1.4142135623730951, 0.0]
[0, 0, 0]
这些都是错误的,我无法弄清楚问题是什么.
These are wrong, and I can't figure out what the problem is.
推荐答案
对于这些几何有专门的数据结构查询,使用经过调试且高效的实现.为什么不使用它们?sklearn.neighbors.BallTree
可以执行这些类型的查询,以及 sklearn.neighbors.KDTree
.
There are specialized data structures for these geometric queries, with implementations that are debugged and efficient. Why not use them? sklearn.neighbors.BallTree
can do these types of queries, as well as sklearn.neighbors.KDTree
.
这篇关于如何在另一个列表中找到距离 (x,y) 位置最近的 (x, y) 位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!