我的问题是我有两个列表一个是排序的,另一个不是排序的,所以我怎样才能得到与排序的索引相同的未排序索引

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

最接近()

最佳答案

使用“distance”函数作为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)

关于python - 变量=不更改值的变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50779986/

10-12 22:00