Python usage notes - iterable stuff
Syntaxish: syntax and language · importing, modules, packages · iterable stuff · concurrency
IO: networking and web · filesystem Data: Numpy, scipy · pandas, dask · struct, buffer, array, bytes, memoryview · Python database notes Image, Visualization: PIL · Matplotlib, pylab · seaborn · bokeh · plotly
Stringy: strings, unicode, encodings · regexp · command line argument parsing · XML |
Sorting
This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me) |
This is stable sorting, so you can do secondary sorting in multiple passes.
You can also do it in one pass, using key
key argument (py2, py3)
The key parameter should be a callable that fetches the value to sort on from an object.
This allows:
- normalization, e.g. case-insensitive sort via:
sorted(['foo','BAR'], key=lambda x: x.lower()) == ['BAR', 'foo']
- specific columns
sorted( [('b',2,),('a',1)], key=lambda l: l[1] ) == [('a', 1), ('b', 2)]
- multiple columns, or other aspects, when you make the key function return a tuple, for example:
- e.g. empty strings last, via:
sorted( ['b',,'A'], key=lambda x: (x==, x.lower())) == ['A', 'b', ]
cmp argument (removed in py3)
You can write an arbitrary comparison function, like
sorted( [[1,2], [2,1]], lambda a,b: cmp(b[1], a[1])) # second column descending
Almost all cmp functions I've seen can be easily rewritten with key (and reverse), possibly decorate-sort-undecorate. In this case:
sorted( [[1,2], [2,1]], key=lambda x: x[1], reverse=True)
As key tends to be faster, cmp is sort of redundant, and python3 removed cmp.
If you've got some cmp functions that are awkward to rewrite, you can use functools.cmp_to_key as a stopgap
See also: