Created
February 26, 2010 02:47
-
-
Save lxneng/315334 to your computer and use it in GitHub Desktop.
Python sorting - A list of objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: import operator | |
In [2]: L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)] | |
In [3]: sorted(L, key=operator.itemgetter(0)) | |
Out[3]: [('a', 4), ('b', 3), ('c', 2), ('d', 1)] | |
In [4]: sorted(L, key=operator.itemgetter(1)) | |
Out[4]: [('d', 1), ('c', 2), ('b', 3), ('a', 4)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment