[Tutor] sorting distances for nearest neighbor

Dave Angel davea at davea.name
Fri Aug 29 14:02:50 CEST 2014


LN A-go-go <lnartist at yahoo.com.dmarc.invalid> Wrote in message:
>
Please tell your email program to use text when posting here, The
 html you used can cause numerous problems. One of them prevents
 me from quoting context.

If you can get your data to a list of tuples, then you can sort
 that list by something like
   sorteddata = sorted (data, key = dist)

Of course you have to write dist. Something like

def dist (tup):
      return distance (tup [3], tup [0], tup [1], tup [2]

But of course this depends greatly on the contents of the tuple. 

You can also skip the sorting if you just want the lowest value by
 doing

     mintuple = min (data, key= dist)

Or if you need the 3 closest,  you can use heapq.nsmallest




-- 
DaveA



More information about the Tutor mailing list