0

I'm trying to figure out how to create a dictionary with the key as the school and values the wins-losses-draws, based on each item in the list. For example, calling my_dict['Clemson'] would return the string "1-1-1" "

team_score_list =[['Georgia', 'draw'], ['Duke', 'loss'], ['Virginia Tech', 'win'], ['Virginia', 'loss'], ['Clemson', 'loss'], ['Clemson', 'win'], ['Clemson', 'draw']]

The output for the above list should be the following dictionary:

{'Georgia': 0-0-1, 'Duke': 0-1-0, 'Virginia Tech': 1-0-0, 'Virginia': 0-1-0, 'Clemson': 1-1-1}

For context, the original data comes from a CSV, where each line is in the form of Date,Opponent,Location,Points For,Points Against.

For example: 2016-12-31,Kentucky,Neutral,33,18.

I've managed to wrangle the data into the above list (albeit probably not in the most efficient manner), however just not exactly sure how to get this into the format above.

Any help would be greatly appreciated!

Cummins070
  • 63
  • 5
  • can you explain your output? how does `Georgia` map to `0-0-1` and `Duke` to `0-1-0`? what of `clemson`? What is the logic? – Onyambu Nov 24 '19 at 18:31
  • Thanks for the response, and sorry if I didn't make that clearer. The first value should be the number of wins, the second should be losses, and the 3rd the number of draws. So for the above list, Georgia only occurs once, and the value is "draw". So the dictionary key should be the school name (Georgia), with the values 0-0-1, meaning 0 wins, 0 losses and 1 draw. Duke on the other hand again appears only once, but the associated value is a loss, therefore it gets coded as 0-1-0 e.g. 0 wins, 1 loss, zero draws. Clemson occurs 3 times, with 1 win, 1 draw and 1 loss, therefore 1-1-1 – Cummins070 Nov 24 '19 at 18:49
  • You have not explained how you get the value '0-0-1'. Why is it 0-0-1 instead of 1-0-0?? – Onyambu Nov 24 '19 at 18:51
  • Sorry was editing while you replied ;) – Cummins070 Nov 24 '19 at 18:54

3 Answers3

0

Not beautiful but this should work.

team_score_list = [
    ["Georgia", "draw"],
    ["Duke", "loss"],
    ["Virginia Tech", "win"],
    ["Virginia", "loss"],
    ["Clemson", "loss"],
    ["Clemson", "win"],
    ["Clemson", "draw"],
]


def gen_dict_lst(team_score_list):
    """Generates dict of list based on team record"""
    team_score_dict = {}
    for team_record in team_score_list:
        if team_record[0] not in team_score_dict.keys():
            team_score_dict[team_record[0]] = [0, 0, 0]
        if team_record[1] == "win":
            team_score_dict[team_record[0]][0] += 1
        elif team_record[1] == "loss":
            team_score_dict[team_record[0]][1] += 1
        elif team_record[1] == "draw":
            team_score_dict[team_record[0]][2] += 1
    return team_score_dict


def convert_format(score_dict):
    """formats list to string for output validation"""
    output_dict = {}
    for key, value in score_dict.items():
        new_val = []
        for index, x in enumerate(value):
            if index == 2:
                new_val.append(str(x))
            else:
                new_val.append(str(x) + "-")
        new_str = "".join(new_val)
        output_dict[key] = new_str
    return output_dict


score_dict = gen_dict_lst(team_score_list)
out_dict = convert_format(score_dict)
print(out_dict)
Brandon
  • 126
  • 6
  • you sir, are a champion. Thank you so much for this! Super helpful to be able to see how to do this. Greatly appreciated :) – Cummins070 Nov 24 '19 at 19:18
0

You can first make a dictionary and insert/increment values of wins,loss and draw while iterating over the dictionary values. Here I have shown a way using variable name same as the string used for win,loss and draw and then increased corresponding value in dictionary using global()['str'] (from another answer)

dct={}
for i in team_score_list:
draw=2
win=0
loss=1
if i[0] in dct:
    dct[i[0]][globals()[i[1]]]+=1
else:
    dct[i[0]]=[0,0,0]
    dct[i[0]][globals()[i[1]]]=1

You can then convert your list to string by using '-'.join(...) to get it in a format you want in the dictionary.

SajanGohil
  • 960
  • 13
  • 26
0

I now get what you mean:

You could do

a = dict()
f = lambda x,s: str(int(m[x]=='1' or j==s))
for (i,j) in team_score_list: 
    m = a.get(i,'0-0-0')
    a[i] = f"{f(0,'win')}-{f(2,'draw')}-{f(4,'loss')}"

{'Georgia': '0-1-0',
 'Duke': '0-0-1',
 'Virginia Tech': '1-0-0',
 'Virginia': '0-0-1',
 'Clemson': '1-1-1'}

Now this is an answer only for this example. If you had many data, it would be good to use a list then join at the end. Eg

b = dict()
g = lambda x,s: str(int(m[x]) + (j==s))
for (i,j) in team_score_list: 
    m = b.get(i,[0,0,0])
    b[i] =[g(0,"win"),g(1,"draw"),g(2,"loss")]

{key:'-'.join(val) for key,val in b.items()}
{'Georgia': '0-1-0',
 'Duke': '0-0-1',
 'Virginia Tech': '1-0-0',
 'Virginia': '0-0-1',
 'Clemson': '1-1-1'}
Onyambu
  • 67,392
  • 3
  • 24
  • 53