Created
May 28, 2016 13:54
-
-
Save leetschau/93dad4e94b8234a7966fd0092266639d to your computer and use it in GitHub Desktop.
Python sorted 函数,当某项不包含排序依据时,可以将fallback值作为自己的排序依据
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
aa = [{'a': 3}, {'a': 2}, {'a': 5}, {'b':8}] | |
sorted(aa, key=lambda rec: rec['a'] if 'a' in rec else 1, reverse=True) | |
#: [{'a': 5}, {'a': 3}, {'a': 2}, {'b': 8}] | |
sorted(aa, key=lambda rec: rec['a'] if 'a' in rec else 6, reverse=True) | |
#: [{'b': 8}, {'a': 5}, {'a': 3}, {'a': 2}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment