python-函数-匿名函数-字典排序

版权声明:所有代码均为自己总结,若有雷同请勿模仿 https://blog.csdn.net/weixin_44253023/article/details/89784570
stus = [{"name": "zhangsan", "age": 18},{"name": "lisi", "age": 19},
        {"name": "wangwu", "age": 17}]
        
#按照年龄进行排序
stus.sort(key = lambda x: x['age'])
print(stus)

#按照名字进行排序
stus.sort(key = lambda x: x['name'])
print(stus)

#左边的key是一个临时变量,右边只能由一个表达式

猜你喜欢

转载自blog.csdn.net/weixin_44253023/article/details/89784570