简体   繁体   English

在python中两个列表之间映射值的有效方法

[英]efficient way to map values between two lists in python

I have two lists. 我有两个清单。 The first list is a coordinate pair list 第一个列表是坐标对列表

[[x1, y1]
 [x2, y2]
 ...
 [xn, yn]]

The second list is a coordinate pair list along with a value associated with each pair 第二个列表是坐标对列表以及与每个对相关联的值

[[x1',y1',v1']
 [x2',y2',v2']
 ...
 [xn',yn',vn']]

I want to find the closest (x',y') in second list for each pair (x,y) in the first list and then map the value v' to (x,y). 我想在第二个列表中为第一个列表中的每个对(x,y)找到最接近的(x',y'),然后将值v'映射到(x,y)。

My current solution is to loop through both lists and compute euclidean distance between every possible coordinate pairs and map to the minimum distance. 我当前的解决方案是遍历两个列表并计算每个可能的坐标对之间的欧几里得距离,并映射到最小距离。 But the original second list has 3 million entries! 但是原始的第二个列表有300万个条目! Is there a more efficient way to achieve this? 有没有更有效的方法来实现这一目标? Thanks. 谢谢。

Try to round the coordinates in the second list. 尝试舍入第二个列表中的坐标。 That will give you lots of small clusters if coordinates. 如果有坐标,那会给你很多小簇。 Use the rounded coordinate as a key in a dict with the list of coordinates+values as value. 将四舍五入的坐标用作字典中的键,将坐标+值列表作为值。

for x,y in first_list:
    x_,y_ = round(x,y)
    l = d[(x_,y_)]
    ... find closest point in l...

With this algorithm, you only need to check a few points. 使用此算法,您只需要检查几点即可。

If the list is empty, you can retry using a more loose rounding. 如果列表为空,则可以使用更宽松的舍入重试。

You could create a spatial map, mapping "areas" to points from your second list that are in this area, for example you can map 4-tuples of x-min, x-max, y-min, y-max to points, like this: 您可以创建空间地图,将“区域”映射到该区域中第二个列表中的点,例如,可以将x-min,x-max,y-min,y-max的4个元组映射到点,像这样:

{(0, 10, 0, 10): [(2, 4, 5), (5, 1, 12), ...], (0, 10, 10, 20): [(4, 14, -1), ...] }

Now you can pick the corresponding area for your point from the first list, eg, if the point is (24, 13) , pick the list corresponding to area (20, 30, 10, 20) . 现在,您可以从第一个列表中选择点的相应区域,例如,如果该点是(24, 13) ,则选择与区域(20, 30, 10, 20) (24, 13)对应的列表。 Of course, the size of those areas could be different depending on the distribution of the points. 当然,这些区域的大小可以根据点的分布而不同。

If there are any points in this area, pick the one with smallest distance to the original point; 如果该区域中有任何点,请选择与原始点距离最短的点; otherwise look at the next "layer" of eight areas around that area, and so forth. 否则,请查看该区域周围八个区域的下一个“层”,依此类推。 Once you've found a point, you should expand one more layer, since there could still be a point in those layers closer to the original point. 找到一个点后,您应该再扩展一层,因为这些层中仍然可能有一个点更接近原始点。 (see Figure) (见图)

在此处输入图片说明

Here, the red dot is the point from the first list, and the blue dots are those from the second list. 这里,红点是第一个列表中的点,蓝点是第二个列表中的点。 The boxes correspond to the areas in the map. 这些框对应于地图中的区域。 Although there are two dots in the area directly corresponding to the original points, there are points in the next "layer" that are closer, but you won't have to search further out. 尽管在区域中有两个与原始点直接对应的点,但下一个“层”中有一些点更接近,但是您不必进一步搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:在两个列表之间匹配字符串切片的有效方法 - Python: Efficient way of matching slices of strings between two lists 在Python中比较3个列表的值的有效方法? - Efficient way to compare the values of 3 lists in Python? 从嵌套为 python 中的字典值的两个列表中检索数据的最有效方法 - Most efficient way to retrieve data from two lists nested as values of dictionary in python 在python中的列表之间映射的最快方法是什么 - What is the quickest way to map between lists in python 计算两个列表字典之间的相似度的最有效方法是什么? - What is the most efficient way of computing similarity between two dictionnaries of lists? 比较python中两个列表索引的方法 - way to compare between index of two lists in python Python:比较两个整数列表的最有效方法 - Python: Most efficient way to compare two lists of integers Python Pandas:在循环中比较两个列表的最有效方法是什么? - Python Pandas: What is the most efficient way to compare two lists in a loop? 比较两个不同长度的列表的有效方法-Python - Efficient way to compare two lists of different lengths - Python 在 Python 中生成和 zip 两个列表的最干净有效的方法 - Most clean and efficient way to generate and zip two lists in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM