我有一个包含坐标的点列表和另一个点.
I've a list of point with coordinates and another point.
列表中的示例:
(45.1531912,5.7184742),(45.1531912,5.7184742),(45.1531113,5.7184544),(45.1525337,5.718298),(45.1525337,5.718298),一点:
(45.1533837,5.7185242)一个功能:
def dist(point1,point2) .... return aDistance是否有一个python内衬(list-comprehension?)在列表中找到一个点,给定函数在该点上返回列表的最小值?
Is there a python one liner (list-comprehension ?) to find a point in the list where a given function returns the minimal value for the list ?
推荐答案min()函数已经接受了key参数,无需列表理解.
The min() function takes a key argument already, no need for a list comprehension.
假设您要在列表中找到最接近原点的点:
Let's say you wanted to find the closest point in the list to the origin:
min(list_of_points, key=lambda p: distance(p, (0, 0)))将找到它(给定一个distance()函数来计算两点之间的距离).
would find it (given a distance() function that calculates the distance between two points).
从文档中:
可选的 key 参数指定一个与list.sort()相同的单参数排序函数.如果提供了key参数,则必须采用关键字形式(例如min(a,b,c,key=func)).
The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, min(a,b,c,key=func)).
更多推荐
Python:在列表中找到函数返回最小值的项目?
发布评论