使用python查找连通坐标

使用python查找连通坐标,python,coordinates,Python,Coordinates,我正在使用python编写代码。我有一个坐标列表,我想知道是否有办法让函数返回每组连接的坐标。例如: coordinates = [[1, 1], [1, 2], [2, 2], [3, 2], [5, 1], [5, 2], [5, 3], [5, 4]] def get_coordinate_neighbors(coordinates); # then you do whatever formula is required # return result get_coordina

我正在使用python编写代码。我有一个坐标列表,我想知道是否有办法让函数返回每组连接的坐标。例如:

coordinates = [[1, 1], [1, 2], [2, 2], [3, 2], [5, 1], [5, 2], [5, 3], [5, 4]]
def get_coordinate_neighbors(coordinates);
   # then you do whatever formula is required #
   return result
get_coordinate_neighbors(coordinates)
# should return [[[1, 1], [1, 2], [2, 2], [3, 2]], [[5, 1], [5, 2], [5, 3], [5, 4]], ...] #
# please only use one parameter which is the coordinates that I need to find the neighbors of #
列表的长度可能不同,坐标也可能不同。
“连接”就像在一场国际象棋比赛中,国王只能移动一个空间。国王移动前的坐标与国王移动后的坐标相连。

您可以迭代坐标,并将每四对添加到新列表中

coordinates = [[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4]]
def group(coordinates):
    newcoordinates = []
    for i in range(2):
        newcoordinates.append(coordinates[:4])
        coordinates = coordinates[4:]
    return newcoordinates
print(group(coordinates))

        

唯一的问题是范围(num)是硬编码的。看看你能不能找到答案。

我建议你读一读你是否将“连接”定义为“具有相同的X坐标”?这不是“连接”的正常定义
itertools.groupby
可以根据您的要求执行操作。如果x或y坐标相距1个坐标