如何按值对嵌套字典进行排序?

时间:2020-07-21 15:19:47

标签: python-3.x sorting dictionary lambda

这是我拥有的代码: 如何根据积分和目标对我的团队进行排序?优先考虑得分。

 myteams={"Spain":{"points":12 , "goals":6} , "germany":{"points":20 , "goals":10} }

答案必须像:

{"germany":{"points":20 , "goals":10} ,"Spain":{"points":12 , "goals":6} }

1 个答案:

答案 0 :(得分:2)

answer = dict(
    sorted(myteams.items(), key=lambda x: (x[1]["points"], x[1]["goals"]), reverse=True)
)