Python字典排序
2017-11-14 11:55
399 查看
Python字典排序
字典不能像列表那样排序,因为字典本身无序~(微笑)环境:python3.5
假定
排序函数:Ur_cmp
排序元素:Ur_key
我们通过排items,即dict.items(),将字典转换成了一个列表
sorted(dict.items(),cmp=Ur_cmp, key=Ur_key, reverse=True)
按值排序
sorted(dict.items(), cmp=Ur_cmp,key=lambda e:e[1], reverse=True)
按键排序
sorted(dict.items(), cmp=Ur_cmp,key=lambda e:e[0])
举例
#字典 {'absolut': -0.056178598492717224, 'abil': 0.076385845892052995, 'abus': -0.12080112436941715} #排序后列表 [('abil', 0.076385845892052995), ('absolut', -0.056178598492717224), ('abus', -0.12080112436941715)]
总结
字典排序就是转化成列表排序,采用items()最后,如果您觉得有用或者有想法的话,欢迎点赞或者评论。 python群:190341254 丁。
相关文章推荐
- python的排序函数sort,sorted在列表排序和字典排序中的应用详解和举例
- python 复习字典排序,字符串, list, tuple常用方法
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- python学习:读写文件和字典排序
- Python中的字典排序
- Python Trie树实现字典排序
- Python中的字典排序
- 【Python】Python对字典排序
- python 字典排序
- 三、Python的字典排序
- Python 字典排序
- python 字典排序
- python---语句与数据结构应用,迭代、迭代字典、字典排序、字典根据键值查找键、sorted排序方法、translate与maketrans、with语句
- python中对字典排序的sorted函数用法
- Python 字典排序
- python列表字典排序
- Python中对字典排序
- Python 字典排序
- python 字典排序
- python 字典排序