Algorithm to read the file and get K-neighbors

I am not sure how to put this algorithm into real python code. But I
hope this would help find the k-neighbors for each star?