简体   繁体   中英

how to get my for loop to compute the distance between coordinates in list

Im writing a program to calculate distances for each element of a list between latitude and longitude from a file, for instance if my points list has five points p1,p2,p3,p4, and p5, I want the program to find the distance between points p1 and p2, p1 and p3, p1 and p4, and p1 and p5. Then compute the distance between p2 and p3, p2 and p4, p2 and p5 and so on.but cant get the output to calculate the distance between all points in my list.

input from file is 62.24186067241206,110.85699280829596 32.59293412296685,170.1144522172567 57.96358548631895,65.88075339171547 24.001352068102108,1.3473208526774272 -64.06840683422311,153.52144681056024

infile=open('test_part2.txt','r')
lines=infile.readlines()
infile.close()
def get_distance(p1,p2):
        x1=p1[0]
        y1=p1[1]
        x2=p2[0]
        y2=p2[1]
        d=(x1-x2)**2+(y1-y2)**2
        d=d**0.5
        return d
print ('Pairwise distance between points:')
points=[]
for line in lines:
    line = line.strip()
    tokens=line.split(",")
    lat=float(tokens[0].strip())
    lon=float(tokens[1].strip())
    point=[]
    point.append(lat)
    point.append(lon)
    points.append(point)
    for i in range(0,len(points)):
        d=get_distance(point,points[i])
        print(d)

expected output is 66.26088847230339 45.17926239576636 115.99441689212239 133.32118853696534 107.27690301949622 168.9856796040263 98.07518883014596 72.92459951226348 150.24213221549 175.82163417554528

The itertools module provides a combinations function which looks ideal for this, assuming your data is a list of tuple pairs, for example:

import itertools as it
#
if __name__ == "__main__":
    points =[(0,0), (4,0), (4,3), (1,1)]
    temp = list(it.combinations(points, 2))
    for pair in temp:
        print(f"{pair} distance = {get_distance(pair[0], pair[1])}")

Gives this output:

(0, 0), (4, 0) = 4.0
(0, 0), (4, 3) = 5.0
(0, 0), (1, 1) = 1.4142135623730951
(4, 0), (4, 3) = 3.0
(4, 0), (1, 1) = 3.1622776601683795
(4, 3), (1, 1) = 3.605551275463989

Edit if you can't use itertools, this might be a workaround:

count = 0
combos = []
while count < len(points):
    temp = [(points[count], x) for x in points[count+1:]]
    combos.extend(temp)
    count += 1
for x in combos:
    print x

The list comprehension is not too hard to break down into for loops if that's what your school requires. Slicing is more or less a must, however.

You are almost there, you just need to go through the points in two loops. I am assuming we don't have a performance issue here. Also, assuming the points are the way you have described earlier.

62.24186067241206,110.85699280829596

32.59293412296685,170.1144522172567

Updated your code

infile = open('test_part2.txt', 'r')
lines = infile.readlines()
infile.close()

def get_distance(p1, p2):
  x1 = p1[0]
  y1 = p1[1]
  x2 = p2[0]
  y2 = p2[1]
  d = (x1 - x2) ** 2 + (y1 - y2) ** 2
  d = d ** 0.5
  return d

print('Pairwise distance between points:')
points = []
for line in lines:
  line = line.strip()
  tokens = line.split(",")
  lat = float(tokens[0].strip())
  lon = float(tokens[1].strip())
  point = [lat, lon]
  points.append(point)

for i in range(0, len(points)):
   for j in range(i+1, len(points)):
      d = get_distance(points[j], points[i])
      print(d)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM