Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Python list using multiple keys

I have searched lot of time for this. I have got some idea about sorting using key parameter.

I have a list of tuple like this. It's got by OpenCV Hough Circle detection.

correctC = [(298, 172, 25), (210, 172, 25), (470, 172, 25), (386, 172, 22), (648, 172, 25), (384, 44, 22), (558, 110, 22), (562, 170, 25), (382, 108, 25), (734, 172, 25), (126, 172, 24), (646, 44, 22), (296, 110, 23), (126, 234, 26), (470, 236, 25), (296, 44, 25), (208, 108, 24), (38, 170, 25), (730, 110, 22), (730, 44, 25), (468, 110, 23), (468, 44, 25), (208, 44, 25), (124, 44, 22), (558, 44, 22), (36, 110, 24), (36, 44, 22), (298, 234, 26), (210, 236, 25), (648, 234, 25), (732, 234, 22), (562, 234, 26), (384, 236, 25), (38, 234, 26), (646, 110, 25), (124, 112, 27)]

It has 3 values. center coordinate(x,y) and radius.

I need to sort all tuples using it's x and y value.

I can do this sorting separately.

xS=sorted(correctC,key=lambda correctC: correctC[0])

This is sort according to x value only.

yS=sorted(correctC,key=lambda correctC: correctC[1])

This is sort according to y value only.

How can I do both(according to x value and y value) using one expression?

I'm using Python 2.7

like image 405
user119o Avatar asked Apr 13 '17 17:04

user119o


People also ask

How do you sort a list with two keys in Python?

To sort a list of objects by two keys in Python, the easiest way is with the key parameter and a tuple of the keys you want to sort by. Just pass the keys you want to sort by as a tuple for your sorting lambda expression.

How do you sort a list with multiple values in Python?

Use sorted() and operator. itemgetter() to sort a list by two fields. Call sorted(a_list, key = k) with k as operator. itemgetter(i, j) to sort the list by the i -th element and then by the j -th element.

How do I sort a list of keys in Python?

First, sort the keys alphabetically using key_value. iterkeys() function. Second, sort the keys alphabetically using the sorted (key_value) function & print the value corresponding to it. Third, sort the values alphabetically using key_value.

How do you sort two times in Python?

@moose, @Amyth, to reverse to only one attribute, you can sort twice: first by the secondary s = sorted(s, key = operator. itemgetter(2)) then by the primary s = sorted(s, key = operator. itemgetter(1), reverse=True) Not ideal, but works.


2 Answers

In this particular case, if you don't care about how the points with equal x, y value are arranged, just calling sort will do the job. Tuples are sorted in lexicographic order.

correctC.sort()

If you want to be more explicit, just do as the other answer tells:

correctC.sort(key=lambda t: (t[0], t[1]))
like image 94
eguaio Avatar answered Oct 12 '22 15:10

eguaio


From what I can see, this helps:

sorted(correctC, key=lambda correctC:[correctC[0],correctC[1]])

Sorted result:

[(36, 44, 22), (36, 110, 24), (38, 170, 25), (38, 234, 26), (124, 44, 22), (124, 112, 27), (126, 172, 24), (126, 234, 26), (208, 44, 25), (208, 108, 24), (210, 172, 25), (210, 236, 25), (296, 44, 25), (296, 110, 23), (298, 172, 25), (298, 234, 26), (382, 108, 25), (384, 44, 22), (384, 236, 25), (386, 172, 22), (468, 44, 25), (468, 110, 23), (470, 172, 25), (470, 236, 25), (558, 44, 22), (558, 110, 22), (562, 170, 25), (562, 234, 26), (646, 44, 22), (646, 110, 25), (648, 172, 25), (648, 234, 25), (730, 44, 25), (730, 110, 22), (732, 234, 22), (734, 172, 25)]
like image 39
trust512 Avatar answered Oct 12 '22 16:10

trust512