码迷,mamicode.com
首页 > 编程语言 > 详细

python Tips(不定期更新)

时间:2015-05-19 00:20:57      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

dictionary sort

1.根据key排序,正向排序

1 sorted(dic.items(), key=lambda d: d[0])

2.根据value排序,反向排序

sorted(dic.items(), key=lambda d: d[1],reverse=True)

3.排序后对原来dictionay没有改变,如果要使用排序后字典,则需将排序后字典赋值给新的变量

dic=[(‘456‘, 90), (‘123‘, 78), (‘78‘, 10)]
newdic=sorted(dic.items(), key=lambda d: d[1],reverse=True)

print newdic
[(‘456‘, 90), (‘123‘, 78), (‘78‘, 10)]

print dic
{‘123‘: 78, ‘456‘: 90, ‘78‘: 10}

 

python Tips(不定期更新)

标签:

原文地址:http://www.cnblogs.com/alicia-Programming/p/4513232.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!