Вот, пожалуйста. Допустим, у вас есть список ввода с этим форматом [[ID, X, Y],[ID, X, Y]]
.
Вы можете просто проходить через каждый гидрант при прохождении через каждый гидрант и вычислять минимальное расстояние между ними. Вам просто нужно иметь некоторую переменную для хранения минимального расстояния для каждого гидранта и ID ближайшего гидранта.
import math # for sqrt calculation
def distance(p0, p1):
""" Calculate the distance between two hydrant """
return math.sqrt((p0[1] - p1[1])**2 + (p0[2] - p1[2])**2)
input = [[0, 1, 2], [1, 2, -3], [2, -3, 5]] # your input list of hydrant
for current_hydrant in input: # loop through each hydrant
min_distance = 999999999999999999999999
closest_hydrant = 0
for other_hydrant in input: # loop through each other hydrant
if current_hydrant != other_hydrant:
curr_distance = distance(current_hydrant, other_hydrant) # call the distance function
if curr_distance < min_distance: # find the closet hydrant
min_distance = curr_distance
closest_hydrant = other_hydrant[0]
print("Closest fire hydrants to the", current_hydrant[0], "is the hydrants",
closest_hydrant, "with the distance of", min_distance) # print the closet hydrant
Поскольку функция расстояния не очень сложна, я переписываю ее, вы можете использовать другую функцию в библиотеке scipy или numpy, чтобы получить расстояние.
Надеюсь, это поможет;)