将一对元组与元组对列表进行比较
Comparing a tuple of a pair to a list of tuple pairs
所以我正在做这个编程作业,目前我一直在比较一个元组中的值对与列表中的元组对。
这些对基本上是 x 和 y 坐标,我需要从列表中找到最接近元组对的一个。例如,给定点 (-4, 3)
和列表 [(10, 6), (1, 7), (6, 3), (1, 9)]
,最接近的是 (1, 7)
.
这些数字总是随着编程的随机部分而变化,但上面的定义是一个函数。这是全部内容:
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)
我想做的是取点中的 x,y,然后取 "other" 点并求出差值,然后用它来求出 "nearest",但我输了我被困住了。重点是用户定义的函数。
假设以下函数计算2点以内的距离:
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)
您可以遍历所有点并找到最小距离:
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
虽然我可以想出一个更 pythonic 的方法:
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)
上述解决方案的总体思路是构建偏函数。该部分函数采用单个参数和 returns 到作为参数给定的点的距离。这可以重写为 lambda other_point: distance(point, other_point)
但这样更漂亮。
请注意,如果您使用空的点列表 nearest(point, [])
调用上面的函数,它将引发 ValueError
。如有必要,您可以为这种情况添加一个 if。
将 min()
与 key
函数一起使用:
#!/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)
所以我正在做这个编程作业,目前我一直在比较一个元组中的值对与列表中的元组对。
这些对基本上是 x 和 y 坐标,我需要从列表中找到最接近元组对的一个。例如,给定点 (-4, 3)
和列表 [(10, 6), (1, 7), (6, 3), (1, 9)]
,最接近的是 (1, 7)
.
这些数字总是随着编程的随机部分而变化,但上面的定义是一个函数。这是全部内容:
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)
我想做的是取点中的 x,y,然后取 "other" 点并求出差值,然后用它来求出 "nearest",但我输了我被困住了。重点是用户定义的函数。
假设以下函数计算2点以内的距离:
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)
您可以遍历所有点并找到最小距离:
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
虽然我可以想出一个更 pythonic 的方法:
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)
上述解决方案的总体思路是构建偏函数。该部分函数采用单个参数和 returns 到作为参数给定的点的距离。这可以重写为 lambda other_point: distance(point, other_point)
但这样更漂亮。
请注意,如果您使用空的点列表 nearest(point, [])
调用上面的函数,它将引发 ValueError
。如有必要,您可以为这种情况添加一个 if。
将 min()
与 key
函数一起使用:
#!/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)