使用多个键对Python列表进行排序

使用多个键对Python列表进行排序

本文介绍了使用多个键对Python列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此花了很多时间.我对使用键参数进行排序有一些了解.

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

我有一个这样的元组列表.它是通过OpenCV Hough Circle检测获得的.

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

它具有3个值.中心坐标(x,y)和半径.

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

我需要使用x和y值对所有元组进行排序.

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

这是根据x值排序的,仅 .

This is sort according to x value only.

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

这是根据y值排序的,仅 .

This is sort according to y value only.

如何使用一个表达式同时执行两个操作(根据x值和y值)?

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

我正在使用Python 2.7

I'm using Python 2.7

推荐答案

据我所知,这很有帮助:

From what I can see, this helps:

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

排序结果:

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

这篇关于使用多个键对Python列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 07:18