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
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.
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.
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.
@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.
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]))
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With