HOME - Recent Changes - Search:

Academic Work


Personal

* pot de départ


dblp


(:twitter:)

-----

[ edit | logout ]
[ help | sandbox | passwd ]

Sorting

#############################

The following examples were taken from http://xahlee.org/perl-python/sort_list.html .

# 1st example
li = [1,9,2,3]
li.sort()   # the list is changed IN PLACE
print li   # [1, 2, 3, 9]


# 2nd example: sort the elems by the embedded numbers
files = ['my283.jpg',
         'my23i.jpg',
         'web7-s.jpg',
         'fris88large.jpg']

def myComp(x, y):
    import re
    def getNum(str):
        return float( re.search(r'(\d+)', str).group(1) )
    return cmp(getNum(x), getNum(y))

files.sort(myComp)
print files   # ['web7-s.jpg', 'my23i.jpg', 'fris88large.jpg', 'my283.jpg']

A better way for the 2nd example:

import re

def my_func(fname):
    return int(re.search(r'\d+', fname).group())

def main():
    files = ['my283.jpg',
        'my23i.jpg',
        'web7-s.jpg',
        'fris88large.jpg']

    print sorted(files, key=my_func)

Descending order:

li = [1,9,2,3]
li.sort(reverse=True)
print li   # [9, 3, 2, 1]

This is much more efficient than sorting in ascending order first and then reversing the list.

Cloud City


anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows


Blogs and Dev.

* Ubuntu Incident
* Python Adventures
* me @ GitHub


Places

Debrecen | France | Hungary | Montreal | Nancy


Notes

full circle | km


Hobby Projects

* Jabba's Codes
* PmWiki
* Firefox
* PHP
* JavaScript
* Scriptorium
* Tutorials
* me @ GitHub


Quick Links


[ edit ]

View - Edit - History - Attach - Print *** Report - Recent Changes - Search
Page last modified on 2012 November 20, 22:19