list of tuples with dynamic change in position

sajuptpm sajuptpm at gmail.com
Tue Sep 7 02:42:27 EDT 2010


More details
I have a list of tuples l = [((cpu_util,mem_util),(disk_util)),
((cpu_util,mem_util),(disk_util))]
ie, l = [((30,50),(70)), ((50,20),(20))]

l.sort(key=lambda x:(-x[0][0], x[1][0])) # sorting cpu_util asc and
disk_util desc

suppose i changed order that is l = [((mem_util,cpu_util),
(disk_util)), ((mem_util,cpu_util),(disk_util))]
So i have to change the sort code to l.sort(key=lambda x:(-x[0][1],
x[1][0])) # sorting cpu_util asc and disk_util desc


I want to use same (common) sort code, that must work even if i
changed tuple order.



More information about the Python-list mailing list