Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the sum of dictionary keys

Score updates consumes a dictionary (see Example) and produces another dict with the value of the string, corresponding to the value of the letters from the dict Scoring. The output looks like Final(see below). This is what I have so far and I'm unsure on how I'm supposed to loop through the string to calculate the sum of it.

Hope you are able to help. Thank you

Example = {'Dallas':"WWLT", 'Seattle':"LLTWWT"}
Final = {'Dallas':5, 'Seattle':6}

def score_updates(weekly_result):
    Scoring = { 'W': 2, 'T': 1, 'L': 0}    
    d = {}
    total = 0
    teams = weekly_result.keys()
    for t in weekly_result:
        total += Scoring[t]
    return d[teams].append(total)
like image 750
Noah Trotman Avatar asked Sep 21 '25 04:09

Noah Trotman


1 Answers

Assuming you already had a dict Scoring, you could just use a dict comprehension with sum.

def score_updates(d):
    return {k: sum(map(Scoring.__getitem__, v)) for k, v in d.items()}
like image 51
miradulo Avatar answered Sep 22 '25 18:09

miradulo