python sorted函数按value值对字典排序
来源:互联网 发布:linux echo $ 编辑:程序博客网 时间:2024/06/15 23:48
1.sorted(d.items(),key=lambda item:item[1] ,reverse=False)
注意得到的是个list,要得到字典前面加dict()
>>> d={"one":45,"two":24,"three":78,"four":98}>>> d.items()dict_items([('one', 45), ('two', 24), ('three', 78), ('four', 98)])>>> sorted(d.items(),key=lambda item:item[1],reverse=False) [('two', 24), ('one', 45), ('three', 78), ('four', 98)]>>> dict(sorted(d.items(),key=lambda item:item[1],reverse=False) ){'two': 24, 'one': 45, 'three': 78, 'four': 98}
2.sorted()传入dict时没有加items,则默认比较的是dict的key,所以在key=function()参数x也是dict的key
from collections import Counterx = Counter({'a':5, 'b':3, 'c':7})sorted(x)>>['a', 'b', 'c']sorted(x.items())>>[('a', 5), ('b', 3), ('c', 7)]tf_counter = Counter(gen_tf(data)) df_counter = Counter(gen_df(data)) vocab = sorted(df_counter, key=lambda x: (df_counter.get(x),tf_counter.get(x)), reverse=True) #注意这儿df_counter没加items(),所有lambda后的x默认是key值
阅读全文
0 0
- python sorted函数按value值对字典排序
- python的sorted函数对字典按key排序和按value排序
- python的sorted函数对字典按key排序和按value排序
- python的sorted函数对字典按key排序和按value排序
- python的sorted函数对字典按key排序和按value排序
- python的sorted函数对字典按key排序和按value排序
- python的sorted函数对字典按key排序和按value排序
- Python sorted 函数,对字典按值进行排序
- python 对字典按value排序
- Python - 字典按值(value)排序
- Python字典按value排序
- python字典排序及sorted函数
- python sorted函数(结合使用lambda和operator.itemgetter()来对字典进行排序)
- Python sorted对字典和列表进行排序
- python学习——采用sorted方法对字典的键、值进行排序
- Python sorted 对字典列表
- python 按字典value排序以及按list中字典的某个key的value排序
- 【Python】Python对字典排序
- hdu2181 哈密顿绕行世界问题【dfs】
- Sublime Text 2.0.2 注册码
- 前端常见的性能优化手段
- HTML总结
- Android 自定义View实例之进度圆环
- python sorted函数按value值对字典排序
- Java内存区域划分
- LeetCode 217. Contains Duplicate
- DevOps介绍
- 清除浮动的方法整理
- eclispe或者myeclispe maven jar包不能部署到tomcat下
- 基于SpringCloud的微服务架构实战案例项目,以一个简单的购物流程为示例
- Android dp和px的转换
- node.js报错