Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What arguments does Python sort() function have?

Is there any other argument than key, for example: value?

like image 554
zjm1126 Avatar asked Dec 29 '09 02:12

zjm1126


People also ask

Does sort take an argument?

The sort() method accepts a reverse parameter as an optional argument.

What does the sort () function do in Python?

Python sorted() Function The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.

How does sort () sort in Python?

Sorting Numbers The function sorted() did not have to be defined. It's a built-in function that is available in a standard installation of Python. sorted() , with no additional arguments or parameters, is ordering the values in numbers in an ascending order, meaning smallest to largest.

Is sort () inbuilt function in Python?

Like C++ sort(), Java sort() and other languages, python also provides built in function to sort. The sort function can be used to sort the list in both ascending and descending order. To sort the list in ascending order.


1 Answers

Arguments of sort and sorted

Both sort and sorted have three keyword arguments: cmp, key and reverse.

L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1

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

Using key and reverse is preferred, because they work much faster than an equivalent cmp.

key should be a function which takes an item and returns a value to compare and sort by. reverse allows to reverse sort order.

Using key argument

You can use operator.itemgetter as a key argument to sort by second, third etc. item in a tuple.

Example

>>> from operator import itemgetter

>>> a = range(5)
>>> b = a[::-1]
>>> c = map(lambda x: chr(((x+3)%5)+97), a)
>>> sequence = zip(a,b,c)

# sort by first item in a tuple
>>> sorted(sequence, key = itemgetter(0))
[(0, 4, 'd'), (1, 3, 'e'), (2, 2, 'a'), (3, 1, 'b'), (4, 0, 'c')]

# sort by second item in a tuple
>>> sorted(sequence, key = itemgetter(1))
[(4, 0, 'c'), (3, 1, 'b'), (2, 2, 'a'), (1, 3, 'e'), (0, 4, 'd')]

# sort by third item in a tuple
>>> sorted(sequence, key = itemgetter(2))
[(2, 2, 'a'), (3, 1, 'b'), (4, 0, 'c'), (0, 4, 'd'), (1, 3, 'e')]

Explanation

Sequences can contain any objects, not even comparable, but if we can define a function which produces something we can compare for each of the items, we can pass this function in key argument to sort or sorted.

itemgetter, in particular, creates such a function that fetches the given item from its operand. An example from its documentation:

After, f=itemgetter(2), the call f(r) returns r[2].

Mini-benchmark, key vs cmp

Just out of curiosity, key and cmp performance compared, smaller is better:

>>> from timeit import Timer
>>> Timer(stmt="sorted(xs,key=itemgetter(1))",setup="from operator import itemgetter;xs=range(100);xs=zip(xs,xs);").timeit(300000)
6.7079150676727295
>>> Timer(stmt="sorted(xs,key=lambda x:x[1])",setup="xs=range(100);xs=zip(xs,xs);").timeit(300000)
11.609490871429443
>>> Timer(stmt="sorted(xs,cmp=lambda a,b: cmp(a[1],b[1]))",setup="xs=range(100);xs=zip(xs,xs);").timeit(300000)
22.335839986801147

So, sorting with key seems to be at least twice as fast as sorting with cmp. Using itemgetter instead of lambda x: x[1] makes sort even faster.

like image 159
sastanin Avatar answered Oct 24 '22 11:10

sastanin