Chams Agouni
Chams Agouni

Reputation: 87

variable = Variable without changing value

My Question is i have two list one is sorted and the another not so how can i get the not sorted index that eqaul to the sorted one

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()

Upvotes: 3

Views: 64

Answers (1)

APerson
APerson

Reputation: 8422

Use your "distance" function as the key argument for whatever sorting function you use.

>>> 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)

Upvotes: 1

Related Questions