我在 Python 中有一個嵌套的 dict 想要排序。
在第一步中,我希望它按“點”排序。
如果所有“點”都相等,則使用“贏”。
如果“積分”和“勝利”相等,則應按“巴西”、“摩洛哥”等球隊名稱排序。
我的字典是
list_of_team = {
"Brazil": {"wins": 1, "loses": 1, "draws": 1, "goal difference": 0, "points": 4},
"Spain": {"wins": 1, "loses": 1, "draws": 1, "goal difference": 0, "points": 4},
"Portugal": {"wins": 1, "loses": 1, "draws": 1, "goal difference": 0, "points": 4},
"Morocco": {"wins": 1, "loses": 1, "draws": 1, "goal difference": 0, "points": 4}
}
輸出應如下所示:
Brazil wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
Morocco wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
Portugal wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
Spain wins:1 , loses:1 , draws:1 , goal difference:0 , points:4
uj5u.com熱心網友回復:
sorted(list_of_team, key= lambda x : list_of_team[x].get("points"))
uj5u.com熱心網友回復:
如果您想按降序按“積分”和“獲勝”排序,并按字典順序按團隊名稱排序:
out = sorted(list_of_team, key= lambda x : (-list_of_team[x].get("points"), -list_of_team[x].get("wins"), x))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/386379.html
上一篇:隨機選擇類別內的字典條目數?
