Home > Back-end >  Get max value from nested dictionary return both keys if equal
Get max value from nested dictionary return both keys if equal

Time:11-02

I have following dictionary

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


}

I want to sum home team and away team goals and find which team won in aggregate if both have same score result is draw. I tried converting to tuple and used max with lambda but it fails when result score is same.

required output for current question: winner! away team

if same goals in all matches in aggregate: Draw

CodePudding user response:

Use this tricky way

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]

CodePudding user response:

Try the below approach

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")

Output

> python -u "run.py"
Winner! Away team
>

CodePudding user response:

There are many options to do this, and one of the ways is to use reduce function

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") 

CodePudding user response:

I provide an intuition way to deal this problem. You can try the following code snippet:

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")
  • Related