概述
{ 'searchResult' : [{ 'resultType' : 'station','ranking' : 0.5 },{ 'resultType' : 'station','ranking' : 0.35 },'ranking' : 0.40 } ] }
并希望得到
{ 'searchResult' : [{ 'resultType' : 'station','ranking' : 0.4 },'ranking' : 0.35 } ] }
尝试了代码没有成功
result = sorted(result.items(),key=lambda k: k[1][0][1]["ranking"],reverse=True)
a = { 'searchResult' : [{ 'resultType' : 'station','ranking' : 0.5 },{ 'resultType' : 'station','ranking' : 0.35 },{ 'resultType' : 'station','ranking' : 0.40 }] } a["searchResult"].sort(key=lambda d: d["ranking"],reverse=True)
或者你可以制作一份深层拷贝以保留原件
from copy import deepcopy srt_dict = deepcopy(a) srt_dict["searchResult"].sort(key=lambda d: d["ranking"],reverse=True)
总结
以上是编程之家为你收集整理的在Python字典中按嵌套字典排序全部内容,希望文章能够帮你解决在Python字典中按嵌套字典排序所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧