代码之家  ›  专栏  ›  技术社区  ›  lord stock

从嵌套字典中获取最大值如果相等则返回两个键

  •  0
  • lord stock  · 技术社区  · 3 年前

    我有以下词典

      {
        "match_1":{
            "home_team":2,
            "away_team":1
        },
        "match_2":{
            "home_team":1,
            "away_team":2
        },
        "match_3":{
            "home_team":1,
            "away_team":4}
    
    
    }
    

    我想总结主队和客场的进球,找出哪支球队总共赢了 如果双方得分相同,则打平。 我试着转换为 tuple 和使用 max 具有 lambda 但当结果得分相同时失败。

    当前问题所需的输出: winner! away team

    如果所有比赛的总进球数相同: Draw

    4 回复  |  直到 3 年前
        1
  •  1
  •   Deepak Tripathi    3 年前

    使用这种棘手的方式

    d={
        "match_1":{
            "home_team":2,
            "away_team":1
        },
        "match_2":{
            "home_team":1,
            "away_team":2
        },
        "match_3":{
            "home_team":1,
            "away_team":4}
    
    
    }
    score = ((i["home_team"], i["away_team"]) for i in d.values())
    t1,t2 = map(sum,zip(*score))
    [["Away team","Home team"][t1>t2], "Draw"][t1==t2]
    
        2
  •  1
  •   Anubhav Sharma    3 年前

    尝试以下方法

    table =   {
        "match_1":{
            "home_team":2,
            "away_team":1
        },
        "match_2":{
            "home_team":1,
            "away_team":2
        },
        "match_3":{
            "home_team":1,
            "away_team":4}
    
    
    }
    score = {}
    for match in table.keys():
        for team in table[match].keys():
            if team in score.keys():
                score[team]+=table[match][team]
            else:
                score[team]=table[match][team]
    
    if score["home_team"]==score["away_team"]:
        print("Draw")
    elif score["home_team"]>score["away_team"]:
        print("Winner! Home team")
    else:
        print("Winner! Away team")
    

    输出

    > python -u "run.py"
    Winner! Away team
    >
    
        3
  •  1
  •   Damzaky    3 年前

    有很多选择,其中一种方法是使用 reduce 作用

    import functools
    
    d = {
        "match_1": {"home_team": 2, "away_team": 1},
        "match_2": {"home_team": 1, "away_team": 2},
        "match_3": {"home_team": 1, "away_team": 4},
    }
    
    result = functools.reduce(
        lambda a, b: {
            "home_team": a["home_team"] + b["home_team"],
            "away_team": a["away_team"] + b["away_team"],
        },
        d.values(),
    )
    
    if result["home_team"] > result["away_team"]:
        print("winner! home_team")
    elif result["away_team"] > result["home_team"]:
        print("winner! away_team")
    else:
        print("Draw") 
    
    
        4
  •  1
  •   Alex Liao    3 年前

    我提供了一种直观的方法来处理这个问题。您可以尝试以下代码片段:

    match =   {
        "match_1":{
            "home_team":2,
            "away_team":1
        },
        "match_2":{
            "home_team":1,
            "away_team":2
        },
        "match_3":{
            "home_team":1,
            "away_team":4}
    }
    
    home_team_score = 0
    away_team_score = 0
    
    for m in match:
        home_team_score += match[m]['home_team']
        away_team_score += match[m]['away_team']
    
    if home_team_score > away_team_score:
        print("winner! home team")
    elif home_team_score < away_team_score: 
        print("winner! away team")
    else:
        print("Draw")