dict.items()返回的是列表,列表中放元组,比如说dict={'a':1,'b':3,'c':2} 用了dict.items得到[(a,1),(b,3),(c,2)]
sorted 一般默认从小到到排序,除非你用reverse=True,是从大到小排序,并且默认是元组中的第一个元素排序,如果不想默认,就用lambda函数定义
注意:sorted()函数用的参数是列表,dict是字典,要用dict.items()把字典转化为列表。但如果本身就是列表就不用转化。
复制代码
1
2
3dict={'a':1,'b':3,'c':2} ls=sorted(dict.items(),reverse=True,key=lambda x:x[1]) print(ls)
复制代码
1
2
3
4
5
6
7ls=[ {'name':'tom','age':13}, {'name':'susan','age':14}, {'name':'koko','age':15}, ] ls=sorted(ls,key=lambda x:x['age'],reverse=True) print(ls)
复制代码
1
2
3ls=[2,4,6,8,10] new_filter=filter(lambda x:x>4,ls) print(list(new_filter))
最后
以上就是危机朋友最近收集整理的关于python sorted,filter的用法的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复