Last active
August 21, 2018 02:43
-
-
Save tanish-kr/696b1176e7da3ea6bad3 to your computer and use it in GitHub Desktop.
【メモ】Python3 list sort ref: https://qiita.com/kitaro_tn/items/7dcd85e610b027b54478
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
list = [1,3,2,4] | |
list.sort() | |
print(list) | |
# [1, 2, 3, 4] |
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
list = [1,3,2,4] | |
print(sorted(list)) | |
# [1, 2, 3, 4] |
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
list = [1,3,2,4] | |
list.sort(reverse=True) | |
print(list) | |
# [4, 3, 2, 1] |
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
dict = {"a","c","d","b"} | |
print(sorted(dict)) | |
# ['a', 'b', 'c', 'd'] |
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
list_dict = [{"no":1,"name":"Devit"},{"no":4,"name":"Josh"},{"no":2,"name":"Sam"},{"no":3,"name":"Tom"}] | |
print(sorted(list_dict,key=lambda x:x["no"])) | |
# [{'name': 'Devit', 'no': 1}, {'name': 'Sam', 'no': 2}, {'name': 'Tom', 'no': 3}, {'name': 'Josh', 'no': 4}] | |
# 降順 | |
print(sorted(list_dict,key=lambda x:x["no"],reverse=True)) | |
# [{'name': 'Josh', 'no': 4}, {'name': 'Tom', 'no': 3}, {'name': 'Sam', 'no': 2}, {'name': 'Devit', 'no': 1}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment