有一字典{"A":5,"C":4,"B":10,"D":20}怎样最快对这个字典按照value降序排列,变成{“D”:20,"B":10,"A":5,"C":4}?
2 回答
幕布斯7119047
TA贡献1794条经验 获得超8个赞
dic={"A":5,"C":4,"B":10,"D":20}print(sorted(dic.items(),key=lambdaa:a[1],reverse=True))
MM们
TA贡献1886条经验 获得超2个赞
字典本身是无序的,需要先转成元组再排序如下:a={"A":5,"C":4,"B":10,"D":20}importoperator#按值排序排序b=sorted(a.items(),key=operator.itemgetter(1),reverse=True)print(b)
添加回答
举报
0/150
提交
取消