这个博客上对此有很多讨论,列出2篇较好的:
http://hi.baidu.com/jrckkyy/blog/item/021849ed8a01e14479f05530.html
http://hi.baidu.com/jrckkyy/blog/item/183933cb3e5c54f653664f12.html
复制一段我觉得以后用得上的
- # 演示对字典数据进行排序
- sampledict_str = {'blue':'[email protected]',
- 'allen':'[email protected]',
- 'sophia':'[email protected]',
- 'ceen':'[email protected]'}
- print sampledict_str
- # 按照key进行排序
- print sorted(sampledict_str.items(), key=lambda d: d[0])
- # 按照value进行排序
- print sorted(sampledict_str.items(), key=lambda d: d[1])