list1=[(1116.7983351235232, 1311.3754027926993), (1116.7983351235232, 1218.42998120301), (1116.7983351235232, 1125.4845596133205), (1116.7983351235232, 1032.539138023631), (1116.7983351235232, 939.5937164339416)]
list2=[(1215.7983351235232, 1315.130773361976), (1215.7983351235232, 1223.1241944146077), (1215.7983351235232, 1131.1176154672394), (1215.7983351235232, 1039.111036519871), (1215.7983351235232, 947.1044575725027)]
For the above list, I want to find the Euclidean distance between a point in list1
and all the points in list2
. Here, I want to find the min distance from the point in list1 to all the points in list2. Could any one suggest what is the best way to work on it?
i tried out
for i in list1:
for j in list2:
list3=min(sqrt(i,j))
print(list3)
I am getting error as AttributeError: 'Tuple' object has no attribute '_eval_power'
I propose doing this step by step:
First, write a function that calculates the euclidean distance between two points. We can do this by using Pythagoras' theorem on the distance between their respective components:
from math import sqrt
def distance(x, y):
a, b = y[0] - x[0], y[1] - x[1]
return sqrt(a**2 + b**2)
Now you can loop over all combinations of points and return all distances. I'll use a generator to do that:
def all_distances(one, two):
for point_a in one:
for point_b in two:
yield distance(point_a, point_b)
Finally, it all falls into place:
>>> min(all_distances(list1, list2))
99.07120069986327
So the minimum distance is (very close to) 99.
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.