python Tips(不定期更新)

时间:2015-05-19 00:20:57   收藏:0   阅读:183

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}

 

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!