[Tutor] Sorting by numbers

Jonas Melian jonasmg at SoftHome.net
Sun Aug 21 12:46:19 CEST 2005


:( I get
 >>> modes
[['85', '640x480'], ['85', '800x600'], ['85', '1024x768'], ['85', 
'1280x1024'], ['70', '1600x1200'], ['60', '1920x1440']]
please help!

>Solution:
>modes.sort(key=lambda item: int(item[1].split('x')[0])) # 2.4
>modes.sort(lambda x, y: cmp(int(x[1].split('x')[0]), 
>int(y[1].split('x')[0]))) #2.3.5
>
>Jonas Melian wrote:
>
>  
>
>>From an input as this:
>>
>>Standard timing 0: 85 Hz, 640x480
>>Standard timing 1: 85 Hz, 800x600
>>Standard timing 2: 85 Hz, 1024x768
>>Standard timing 3: 85 Hz, 1280x1024
>>Standard timing 4: 70 Hz, 1600x1200
>>Standard timing 5: 60 Hz, 1920x1440
>>
>>I want to get columns 3 and 5 for sort them from mayor mo minor, so:
>>
>>85 1280x1024
>>85 1024x768
>>85 800x600
>>85 640x480
>>70 1600x1200
>>60 1920x1440
>>
>>------
>>modes = []
>>i = 3; j = 5  # Columns for get
>>for ln in data:
>>   if ln.startswith('Standard'):
>>      modes.append(ln.split()[i:j+1:j+1-i-1])
>>
>>    
>>
>>>>>modes
>>>>>          
>>>>>
>>[['85', '640x480'], ['85', '800x600'], ['85', '1024x768'], ['85', 
>>'1280x1024'], ['70', '1600x1200'], ['60', '1920x1440']]
>>    
>>
>>>>>modes.sort()
>>>>>modes
>>>>>          
>>>>>
>>[['60', '1920x1440'], ['70', '1600x1200'], ['85', '1024x768'], ['85', 
>>'1280x1024'], ['85', '640x480'], ['85', '800x600']]
>>-------
>>
>>Well, it's from minor to mayor.
>>But the big problem is that Python does lexicographic sorting on tuples. 
>>So, how sort the second column by the numbers before of 'x'?
>>
>>I'm supossing that there is to use split()
>>
>>Any help? please
>>Thanks in advance!
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>> 
>>
>>    
>>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>



More information about the Tutor mailing list