Count how many times football teams have won using the nested list

I need to write a function that looks at a nested list with two teams and their game score accordingly. The list contains multiple matches and I want the result to be a nested list with all the team names and the number of games won. The list looks like this:

L = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', 2'], ['Giants', 'Steelers', '3', '5']]

      

So in the above list, the first two items are the names of the teams, and the third and fourth items are the points they score in the match. However, the list is much longer than this one, and there are many more commands. The result will look something like this:

finalList = [['Patriots', 2], ['Giants', 0], ['Steelers', 1]]

      

because the Patriots won two games, the Giants won zero games, and the Steelers won one game.

I tried the following code but it doesn't work and I am stuck.

def gamesWon():
    for i in L:
        count = 0
        if i[2]>i[3]:
            count += 1
            i.append(count)

      

+3


source to share


4 answers


You can use defaultdict

:

from collections import defaultdict
# initialize the result as a defaultdict with default value of 0
result = defaultdict(lambda : 0)   

for t1,t2,s1,s2 in L:
    if int(s1) > int(s2):
        result[t1] += 1
    elif int(s2) > int(s1):
        result[t2] += 1

result
# defaultdict(<function __main__.<lambda>>, {'Patriots': 2, 'Steelers': 1})

      



Note that although there are no zero-rated commands as a result, if you call result[team]

, it gives zero.

+3


source


you can use defaultdict

from collections import defaultdict

L = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', '2'], ['Giants', 'Steelers', '3', '5']]

D = defaultdict(int)

for match in L:
    team1, team2, score1, score2 = match
    D[team1] # make sure the team exist in the dict even if it never wins a match
    D[team2] # make sure the team exist in the dict even if it never wins a match
    if int(score1) > int(score2):
        D[team1] += 1
    if int(score2) > int(score1):
        D[team2] += 1

      



Then you can easily convert D

to a list if you absolutely need to ...

+2


source


Alternatively, you can use Counter

, which is dict-like:

import collections as ct

L = [
    ['Patriots', 'Giants', '3', '1'], 
    ['Steelers', 'Patriots', '1', '2'], 
    ['Giants', 'Steelers', '3', '5'],
    ['Giants', 'Patriots', '1', '1']                       # tie  
]    

def count_wins(games):
    """Return a counter of team wins, given a list of games."""
    c = ct.Counter()                                        
    for team1, team2, s1, s2 in games:
        c[team1] += 0
        c[team2] += 0
        if int(s1) == int(s2):
            continue
        elif int(s1) > int(s2):
            c[team1] += 1
        else:
            c[team2] += 1
    return c

season = count_wins(L)
season
# Counter({'Giants': 0, 'Patriots': 2, 'Steelers': 1})

      


The last code gives zero increments by default for new records and handles links:

L_tie = [['Cowboys', 'Packers', '3', '3']]
game = count_wins(L_tie)
game
# Counter({'Cowboys': 0, 'Packers': 0})

      

Counters have several useful methods for finding the best teams:

season.most_common(2)
# [('Patriots', 2), ('Steelers', 1)]

      

Counters are flexible. You can easily update the counters:

season.update(game)
season
# Counter({'Cowboys': 0, 'Giants': 0, 'Packers': 0, 'Patriots': 2, 'Steelers': 1})

      

You can also add (subtract and perform set operations with) other counters:

L_last = [['49ers', 'Raiders', '7', '10'], ['Packers', 'Patriots', '3', '7']] 
last_season = count_wins(L_last)
season + last_season
# Counter({'Patriots': 3, 'Raiders': 1, 'Steelers': 1})

      

UPDATE: See also this linked answer for the Counter

/ generator expression variant .

0


source


ll = [['Patriots', 'Giants', '3', '1'], ['Steelers', 'Patriots', '1', '2'], ['Giants', 'Steelers', '3', '5']]

teamStatus = {}

for l in ll:
  team1,team2,team1_score,team2_score = l
  if team1 not in teamStatus:
      teamStatus[team1] = 0
  if team2 not in teamStatus:    
      teamStatus[team2] = 0

  if int(team1_score) > int(team2_score):
    teamStatus[team1] += 1 
  else:
    teamStatus[team2] += 1

print(teamStatus)

      

RESULT

{'Patriots': 2, 'Giants': 0, 'Steelers': 1}

      

0


source







All Articles