Sort Sorted use built-in functions
Sorted sorted list
1, to sort the list in reverse order
# 对列表进行降序序排序
list = [1,3,4,23,6,7]
list = sorted(list,reverse=True)
print(list)
Print Results:
[1, 3, 4, 6, 7, 23]
2, positive sequence to sort the list
# 对列表进行升序排序
list = [1,3,4,23,6,7]
list = sorted(list)
print(list)
Print Results:
[23, 7, 6, 4, 3, 1]
Sorted sort dictionary
1, a dictionary Key value in descending order
Key parameters Sorted function using the lambda = Key dict: dict [0], the lambda expression is meant to select the first tuple element as comparison parameter (dict [0] <=> sort key)
# 对字典中的key值进行排序
dict = {"age1":18,"age2":80,"age3":5,"age4":17,"age5":26}
dict_sorted = sorted(dict1.items(),key=lambda dict:dict[0])
print(dict_sorted)
Print Results:
[('age1', 18), ('age2', 80), ('age3', 5), ('age4', 17), ('age5', 26)]
2, the value of Value in the dictionary in descending order
Key parameters Sorted function using the lambda = Key dict: dict [. 1], the lambda expression is meant to select the second tuple element as comparison parameter (dict [. 1] <=> sorted Value)
dict = {"age1":18,"age2":80,"age3":5,"age4":17,"age5":26}
dict_sorted = sorted(dict1.items(),key=lambda dict:dict[1])
print(dict_sorted)
Print Results:
[('age3', 5), ('age4', 17), ('age1', 18), ('age5', 26), ('age2', 80)]