Creative Commons License
This blog by Tommy Tang is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

My github papge

Monday, April 8, 2013

custom sort in python, the built-in sorted()

let's say you have a list, and you want to sort it. you can use the build in function sorted()


Help on built-in function sorted in module __builtin__:

sorted(...)
    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
(END)


it takes any iterable as input.
for example:

>>> a=[4,2,1,6]
>>> sorted(a)
[1, 2, 4, 6]

>>> sorted(a, reverse=True)
[6, 4, 2, 1]

>>> a=['ccc','aaaa','d','bb']
>>> sorted(a)
['aaaa', 'bb', 'ccc', 'd']

if you want to sort according to the length of the string, pass a len function as the argument
>>> sorted(a, key=len)
['d', 'bb', 'ccc', 'aaaa']

>>> a=['ccc','aaaz','a','bb']
you want to sort according to the last letter of the string

>>> def last(s):
...     return s[-1]
... 
>>> sorted(a, key=last)
['a', 'bb', 'ccc', 'aaaz']


let's say you have a list of tuples, the sorted function sorts according to the first element 
of the tuple, if they are the same, compare the second element of the tuple.

>>> a=[(1,'b'),(2,'a'),(1,'a')]
>>> sorted(a)
[(1, 'a'), (1, 'b'), (2, 'a')]

so, if you want to sort by something first and then sort by another thing next, you can write
a key function to return a tuple.














No comments:

Post a Comment