how to sort a list of tuples with custom function

Piet van Oostrum piet-l at vanoostrum.org
Tue Aug 1 17:10:06 EDT 2017


Ho Yeung Lee <jobmattcon at gmail.com> writes:

> def isneighborlocation(lo1, lo2):
>     if abs(lo1[0] - lo2[0]) < 7  and abs(lo1[1] - lo2[1]) < 7:
>         return 1
>     elif abs(lo1[0] - lo2[0]) == 1  and lo1[1] == lo2[1]:
>         return 1
>     elif abs(lo1[1] - lo2[1]) == 1  and lo1[0] == lo2[0]:
>         return 1
>     else:
>         return 0
>
>
> sorted(testing1, key=lambda x: (isneighborlocation.get(x[0]), x[1]))
>
> return something like
> [(1,2),(3,3),(2,5)]

I think you are trying to sort a list of two-dimensional points into a
one-dimensiqonal list in such a way thet points that are close together
in the two-dimensional sense will also be close together in the
one-dimensional list. But that is impossible.
-- 
Piet van Oostrum <piet-l at vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list