3

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

Chams Agouni
  • 87
  • 11
  • Possible duplicate of [How to get indices of a sorted array in Python](https://stackoverflow.com/questions/6422700/how-to-get-indices-of-a-sorted-array-in-python) – James Jun 10 '18 at 02:07
  • I dont understand can you give me a full code – Chams Agouni Jun 10 '18 at 02:10
  • 1
    The previous message is automatically generated when I noted that your question is a duplicate of a previously answered question. You are asking how to do an argsort in python. Take a look at the question and answer in the link. – James Jun 10 '18 at 02:13
  • it dosnt work it just give me the orignal array like 0 1 2 3 4 5 – Chams Agouni Jun 10 '18 at 02:17
  • Add `print([i[0] for i in sorted(enumerate(a), key=lambda x:x[1])])` before `a.sort()` – James Jun 10 '18 at 02:19
  • still the same i need (54, 0) instead of the sorted – Chams Agouni Jun 10 '18 at 02:22

1 Answers1

1

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)
APerson
  • 8,140
  • 8
  • 35
  • 49