# 函数介绍

## sorted()

sorted函数：

sorted(iterable[,key][,reverse])

## lambda

lambda 匿名函数

lambda arguments: expression

def <lambda>(arguments):
return expression

# 排序

## 1 简单列表(list)排序

list = ['a', 'b', 'c']
print(sorted(list))
# ['a', 'b', 'c']
print(sorted(list, reverse=True))
# ['c', 'b', 'a']

## 2 字典(dict)的键(key)排序

dict = {'c': 1, 'b': 2, 'a': 3}
print(sorted(dict))
# ['a', 'b', 'c']
print(sorted(dict, reverse=True))
# ['c', 'b', 'a']

## 3 字典(dict)的值(value)排序

dict = {'c': 1, 'b': 2, 'a': 3}
print(sorted(dict, key=lambda k: dict[k]))
# ['c', 'b', 'a']
print(sorted(dict, key=lambda k: dict[k], reverse=True))
# ['a', 'b', 'c']

## 4列表(list)内嵌套列表(list)排序

list = [[4, 2, 9], [1, 5, 6], [7, 8, 3]]
# 以列表中列表的第一个数排序
print(sorted(list, key=lambda k: k[0]))
# [[1, 5, 6], [4, 2, 9], [7, 8, 3]]
# 以列表中列表的第二个数排序
print(sorted(list, key=lambda k: k[1]))
# [[4, 2, 9], [1, 5, 6], [7, 8, 3]]
# 以列表中列表的第三个数排序
print(sorted(list, key=lambda k: k[2]))
# [[7, 8, 3], [1, 5, 6], [4, 2, 9]]
# 以列表中列表的第一个数排序，且降序
print(sorted(list, key=lambda k: k[0], reverse=True))
# [[7, 8, 3], [4, 2, 9], [1, 5, 6]]

## 5字典(dict)内嵌套字典(dict)排序

dict = {
'a': {'x': 3, 'y': 2, 'z': 1},
'b': {'x': 2, 'y': 1, 'z': 3},
'c': {'x': 1, 'y': 3, 'z': 2}
}
# 以内部字典的'x'对应的值排序
print(sorted(dict, key=lambda k: dict[k]['x']))
# ['c', 'b', 'a']
# 以内部字典的'y'对应的值排序
print(sorted(dict, key=lambda k: dict[k]['y']))
# ['b', 'a', 'c']
# 以内部字典的'z'对应的值排序
print(sorted(dict, key=lambda k: dict[k]['z']))
# ['a', 'c', 'b']
# 以内部字典的'x'对应的值排序,并降序
print(sorted(dict, key=lambda k: dict[k]['x'], reverse=True))
# ['a', 'b', 'c']

## 6 列表(list)中嵌套字典(dict)排序

list = [
{'x': 3, 'y': 2, 'z': 1},
{'x': 2, 'y': 1, 'z': 3},
{'x': 1, 'y': 3, 'z': 2},
]
print(sorted(list, key=lambda k: k['x']))
# [{'z': 2, 'x': 1, 'y': 3}, {'z': 3, 'x': 2, 'y': 1}, {'z': 1, 'x': 3, 'y': 2}]
print(sorted(list, key=lambda k: k['y']))
# [{'z': 3, 'x': 2, 'y': 1}, {'z': 1, 'x': 3, 'y': 2}, {'z': 2, 'x': 1, 'y': 3}]
print(sorted(list, key=lambda k: k['z']))
# [{'z': 1, 'x': 3, 'y': 2}, {'z': 2, 'x': 1, 'y': 3}, {'z': 3, 'x': 2, 'y': 1}]
print(sorted(list, key=lambda k: k['x'], reverse=True))
# [{'z': 1, 'x': 3, 'y': 2}, {'z': 3, 'x': 2, 'y': 1}, {'z': 2, 'x': 1, 'y': 3}]

## 7字典(dict)中嵌套列表(list)排序

dic = {
'a': [1, 2, 3],
'b': [2, 1, 3],
'c': [3, 1, 2],
}
print(sorted(dic, key=lambda k: dic[k][0]))
# ['a', 'b', 'c']
print(sorted(dic, key=lambda k: dic[k][1]))
# ['b', 'c', 'a']
print(sorted(dic, key=lambda k: dic[k][2]))
# ['c', 'b', 'a']
print(sorted(dic, key=lambda k: dic[k][0], reverse=True))
# ['c', 'b', 'a']

# 方法解释

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客