Reputation: 57
So I am working on this programming homework and I am currently stuck on comparing the pairs of values in one tuple to the tuple pairs in a list.
The pairs are basically x and y coordinates and I need to find the closest one from the list to the tuple pair. As an example, given the point (-4, 3)
and the list [(10, 6), (1, 7), (6, 3), (1, 9)]
, the closest one would be (1, 7)
.
These numbers always changes with a randoming part of the programming but the the above is defined as a function. Here is the whole thing:
def nearest(point, more_points):
'''
Finds the nearest point to the base point
'''
(x, y) = point
for i, j in more_points:
a = math.fabs(x - i)
tempI = i
b = math.fabs(y - j)
tempJ = j
tempA, tempB = a , b
if min(tempA) < a:
point = ()
my_points = []
c = 0
lpoint = list(point)
while c < 2:
lpoint.append(random.randrange(-5,5,1)) # generate the "point"
c += 1
tpoint = tuple(lpoint)
c = 0
colx = [] # x points
coly = [] # y points
# generate the points
while c < 4:
colx.append(random.randint(0,10))
coly.append(random.randint(0,10))
c += 1
my_point = list(zip(colx,coly))
print(my_point)
the_nearest = nearest(tpoint,my_point)
print(the_nearest)
What I am trying to do is to take the x,y in the point and then take the "other" point and get the difference and then use that to find the "nearest" but I lost and I am stuck. The focus is on the user defined function.
Upvotes: 1
Views: 793
Reputation: 5821
Use min()
with a key
function:
#!/usr/bin/env python3
import math
from functools import partial
def distance_between_points(a, b):
ax, ay = a
bx, by = b
return math.sqrt(pow(ax - bx, 2) + pow(ay - by, 2))
def nearest(point, more_points):
'''
Finds the nearest point to the base point
'''
distance_to_point = partial(distance_between_points, point)
return min(more_points, key=distance_to_point)
point = (-4, 3)
my_points = [(10, 6), (1, 7), (6, 3), (1, 9)]
n = nearest(point, my_points)
print(n)
Upvotes: 0
Reputation: 8061
Assuming the following function calculate the distance within 2 points:
def distance(point_a, point_b):
"""Returns the distance between two points."""
x0, y0 = point_a
x1, y1 = point_b
return math.fabs(x0 - x1) + math.fabs(y0 - y1)
You can iterate over all points and find the minimum distance:
def nearest(point, all_points):
closest_point, best_distance = None, float("inf")
for other_point in all_points:
d = distance(point, other_point)
if d < best_distance:
closest_point, best_distance = other_point, d
return closest_point
Although I could come up with a more pythonic approach:
def nearest(point, all_points):
"""Returns the closest point in all_points from the first parameter."""
distance_from_point = functools.partial(distance, point)
return min(all_points, key=distance_from_point)
The overall idea of the above solution is to build a partial function. This partial function takes a single parameter and returns the distance to the point given as parameter. This is could be re-written as lambda other_point: distance(point, other_point)
but this is prettier.
Note the above function will raise ValueError
if you call it with an empty list of points: nearest(point, [])
. You can add an if for this case if necessary.
Upvotes: 5