Python dict词典排序

来源:互联网 发布:门板雕刻生产软件 编辑:程序博客网 时间:2024/05/21 08:54

整理一下key排序和value排序

  • key
tmpdict = {"1":20,"2":50,"3":40,"4":100,"5":80}# key从大到小>>> for i in sorted(tmpdict.keys(),reverse=True):...     print([i,tmpdict[i]])... ['5', 80]['4', 100]['3', 40]['2', 50]['1', 20]
  • value
>>> tmpdict = {"1":20,"2":50,"3":40,"4":100,"5":80}# value从大到小>>> orderdict = sorted(tmpdict.items(),key=lambda item:item[1],reverse=True)>>> orderdict[('4', 100), ('5', 80), ('2', 50), ('3', 40), ('1', 20)]
原创粉丝点击