Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable = Variable without changing value

Tags:

python

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

like image 227
Chams Agouni Avatar asked Jul 25 '26 23:07

Chams Agouni


1 Answers

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)
like image 119
APerson Avatar answered Jul 28 '26 14:07

APerson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!