python 中 列表(list)和字典(dict)數據排序

這個博客上對此有很多討論,列出2篇較好的: 

http://hi.baidu.com/jrckkyy/blog/item/021849ed8a01e14479f05530.html

 http://hi.baidu.com/jrckkyy/blog/item/183933cb3e5c54f653664f12.html

 

複製一段我覺得以後用得上的

  1. # 演示對字典數據進行排序  
  2. sampledict_str = {'blue':'[email protected]',  
  3.                    'allen':'[email protected]',  
  4.                    'sophia':'[email protected]',  
  5.                    'ceen':'[email protected]'}  
  6. print sampledict_str  
  7. # 按照key進行排序  
  8. print sorted(sampledict_str.items(), key=lambda d: d[0])  
  9. # 按照value進行排序  
  10. print sorted(sampledict_str.items(), key=lambda d: d[1])  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章