[Tutor] Sorting by numbers

Jonas Melian jonasmg at SoftHome.net
Sun Aug 21 12:02:24 CEST 2005


 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!



More information about the Tutor mailing list