我的问题是我有两个列表一个是排序而另一个不是这样我怎样才能得到未排序的索引eqaul到排序的
from math import sqrt as sq
Points = [(54,0), (4,6), (-1,6), (5,-7), (5,1)]
def closeest() :
a= list(int(sq(x[0]**2 + x[1]**2)) for x in Points)
a.sort()
print("The new list is:")
print(a)
print("The closest point is:")
# Here i need to get the value of Points index that equal to the first sorted a list
closeest()
答案 0 :(得分:1)
使用“距离”函数作为key
参数,用于您使用的任何排序功能。
>>> Points = [(54,0), (4,6), (-1,6), (5,-7), (5,1)]
>>> from math import sqrt as sq
>>> dist = lambda point: int(sq(point[0]**2 + point[1]**2))
>>> max(Points, key=dist)
(54, 0)