import numpy as np
from scipy.spatial.distance import cdist

coordinates = [np.array([[300, 2300],
                         [670, 2360],
                         [400, 2300]]),
               np.array([[1500, 1960],
                         [1620, 2200],
                         [1505, 1975]]),
               np.array([[980, 1965],
                         [1060, 2240],
                         [1100, 2250],
                         [980, 1975]]),
               np.array([[565, 1940],
                         [680, 2180],
                         [570, 1945]])]


lasts = [arr[-1] for arr in coordinates]
firsts = [arr[0] for arr in coordinates]

res, coordinates = [coordinates[0]], coordinates[1:]

while coordinates:
    lasts = res[-1][-1].reshape(1, -1)
    distances = cdist(lasts, firsts).flatten()
    nearest = np.argmin(distances)
    res.append(coordinates.pop(nearest))
    firsts.pop(nearest)


print(res)
