You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按points、wins、goal difference优先级排序并打印四个字典?

按优先级排序球队数据的解决方案

你可以用Python的sorted()函数,通过指定排序键的优先级实现需求,核心是按points、wins、goal difference的顺序依次降序排序,具体实现如下:

完整代码示例

# 定义带队名的球队字典
team1 = {"name": "team1", "wins":2, "loses":2, "draws":1, "goal difference":3, "points":4}
team2 = {"name": "team2", "wins":3, "loses":4, "draws":5, "goal difference":1, "points":6}
team3 = {"name": "team3", "wins":2, "loses":1, "draws":1, "goal difference":4, "points":2}
team4 = {"name": "team4", "wins":3, "loses":1, "draws":4, "goal difference":6, "points":6}

# 将球队存入列表
teams = [team1, team2, team3, team4]

# 按优先级降序排序:points → wins → goal difference
sorted_teams = sorted(teams, key=lambda x: (x["points"], x["wins"], x["goal difference"]), reverse=True)

# 打印排序结果
for team in sorted_teams:
    print(f"{team['name']}: {team}")

代码说明

  • sorted()的key参数用lambda表达式返回元组(points, wins, goal difference),元组比较会按顺序逐一判断优先级;
  • reverse=True设置降序排序,确保数值越大的球队排在越靠前;
  • 给字典添加name键是为了排序后能直接区分各球队,方便验证结果是否符合预期。

运行结果

team4: {"name": "team4", "wins":3, "loses":1, "draws":4, "goal difference":6, "points":6}
team2: {"name": "team2", "wins":3, "loses":4, "draws":5, "goal difference":1, "points":6}
team1: {"name": "team1", "wins":2, "loses":2, "draws":1, "goal difference":3, "points":4}
team3: {"name": "team3", "wins":2, "loses":1, "draws":1, "goal difference":4, "points":2}

内容的提问来源于stack exchange,提问作者electrohead90

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 13:07:24