python 字典的值是列表_python实现求和python如何通过列表中字典的值对列表进行排序...
一. 按字典值排序(默認為升序)
x = {1:2, 3:4, 4:3, 2:1, 0:0}
1. sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1)) print sorted_x
輸出結果:[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
如果要降序排序,可以指定reverse=True
電腦技術002pc網從python實現求和python如何通過列表中字典的值對列表進行排序分析來看,對python實現求和python如何通過列表中字典的值對列表進行排序的結果。
2. sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1), reverse=True) print sorted_x
輸出結果:[(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]
二. 或者直接使用list的reverse方法將sorted_x順序反轉
sorted_x.reverse()
三. 更為常用的方法是,用lambda表達式
sorted_x = sorted(x.iteritems(), key=lambda x : x[1]) print sorted_x
輸出結果:[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
orted_x = sorted(x.iteritems(), key=lambda x : x[1], reverse=True) print sorted_x
輸出結果:[(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]
以上就是python如何通過列表中字典的值對列表進行排序的詳細內容,更多請關注其它相關文章!
更多:python實現求和python如何通過列表中字典的值對列表進行排序
https://www.002pc.comhttps://www.002pc.com/python/1449.html
你可能感興趣的列表,python,字典,排序,如何,通過
No alive nodes found in your cluster
0踩
賞
0 贊
總結
以上是生活随笔為你收集整理的python 字典的值是列表_python实现求和python如何通过列表中字典的值对列表进行排序...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python怎样实现封装_大牛教你如何封
- 下一篇: 新概念一册电子书课本_新概念英语第二册完