猿问
在线等,挺急的!python怎样对字典排序?
有一字典{"A":5,"C":4,"B":10,"D":20}怎样最快对这个字典按照value降序排列,变成{“D”:20,"B":10,"A":5,"C":4}?
慕码人2483693
浏览 359
回答 2
2回答
撒科打诨
dic={"A":5,"C":4,"B":10,"D":20}print(sorted(dic.items(),key=lambdaa:a[1],reverse=True))
0
0
0
BIG阳
字典本身是无序的,需要先转成元组再排序如下:a={"A":5,"C":4,"B":10,"D":20}importoperator#按值排序排序b=sorted(a.items(),key=operator.itemgetter(1),reverse=True)print(b)
0
0
0
随时随地看视频
慕课网APP
相关分类
JavaScript
我要回答