简体   繁体   English

如何在列表中找到离我的点最近的两个点的坐标

[英]How do I find the coordinates of the two closest points to my point in a list

I have a list of values and a given point and I want to find the coordinates of the two closest points (one inferior and one superior).我有一个值列表和一个给定点,我想找到两个最近点的坐标(一个下级和一个上级)。

For example:例如:

list = [0, 0.5, 1, 1.5, 2, 2.5, 3]
point = 0.7

I want my return value to be [1, 2] since 0.7 is between 0.5 and 1.我希望我的返回值为 [1, 2],因为 0.7 介于 0.5 和 1 之间。

Any idea of how to do this?知道如何做到这一点吗?

Assuming the given list is always sorted as it is in your example and that it has for sure one value below and one value above your given point, then you can loop over the list until you find a value larger than the given point and then return a list containing that index and the index below it.假设给定列表始终按照您的示例中的方式排序,并且它肯定在给定点下方有一个值,在给定点上方有一个值,那么您可以遍历列表,直到找到一个大于给定点的值,然后返回包含该索引及其下方索引的列表。

def two_closest(_list, point):
    for index, value in enumerate(_list):
        if value > point:
            return (index-1, index)
    raise Exception("Malformed parameters")

For larger lists, you could speed this up with binary search or other heuristics.对于较大的列表,您可以使用二分搜索或其他启发式方法加快速度。 For example, you said your list is created by np.linespace, you could find the exact place your coordinate would lie between if you knew how the list was created.例如,您说您的列表是由 np.linespace 创建的,如果您知道列表是如何创建的,您可以找到您的坐标所在的确切位置。

you can compute distances and then exploit numpy.argsort :您可以计算距离,然后利用numpy.argsort

import numpy as np

def closest_n_indexes(distances, n=2):
    return np.argsort(distances)[:n]

point_list = [0, 0.5, 1, 1.5, 2, 2.5, 3]
point = 0.7

distances = [abs(p-point) for p in point_list]

closest_n_indexes(distances)
Out[8]: array([1, 2])

If list is in sorted order you can use python's built-in binary search library.如果list按排序顺序,您可以使用 python 的内置二进制搜索库。 It's very fast:它非常快:

import bisect

def get_bracket(lst, target):
    lindex = bisect.bisect_right(lst, target) - 1
    rindex = lindex + 1
    return [lindex, rindex]

print(get_bracket([0, 0.5, 1, 1.5, 2, 2.5, 3], 0.7))

which prints:打印:

[1, 2]

暂无
暂无

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

相关问题 对于给定的点,如何在点列表中找到最近的点? - For a given point, how to find closest point in a list of points? 给定两个二维点列表,如何为第一个列表中的每个点找到第二个列表中最近的点? - Given two lists of 2d points, how to find the closest point in the 2nd list for every point in the 1st list? 如何在一组点中找到最接近另一个的点? - How to find point closest to another in a group of points? 在道路网络坐标列表上找到最接近给定点的点 - Find the closest point on a road network coordinates list to a given point 在图像中绘制任意点时,如何找到垂直于该点的两条最近线的坐标? - When drawing an arbitrary point in an image, how to find the coordinates of the two closest lines perpendicular to that point? 如何从点列表中找到最近的坐标? - How to find the closest coordinate from a list of points? 给定两个坐标 a 和 b,我如何找到一个点,该点距 a 的航向为 b 的距离为 x? - Given two coordinates, a and b, how do I find a point that is x distance from a heading towards b? 如何在 pandas dataframe 中查找查询数据点的最近数据点? - How to Find Closest data points for a query data point in a pandas dataframe? 如何在列表中找到比我的号码大的最接近的号码? - How do I find the closest number in a list that is greater than my number? Shapely中两个几何的最近点的坐标 - Coordinates of the closest points of two geometries in Shapely
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM