Computing win/loss records in Python

  • Thread starter Christopher McComas
  • Start date
C

Christopher McComas

Greetings,

I have code that I run via Django that grabs the results from various sports from formatted text files. The script iterates over every line in the formatted text files, finds the team in the Postgres database updates their w/l record depending on the outcome on that line, saves the team's row in the db, and then moves on to the next line in the file.

I'm trying to get away from Django for this project, I want to run the files, get the W/L results and output a formatted text file with the teams and their W/L records. What's confusing me I guess how to store the data/results as the wins and losses tally up. We're talking hundreds of teams, thousands of games, but a quick example would be:

Marshall
Ohio State
Kentucky
Indiana

Marshall,24,Ohio State,48,
Kentucky,14,Indiana,10,
Marshall,10,Indiana,7,
Ohio State,28,Kentucky,10

That's just a quick example, I can handle seperating the data in the lines, figuring it all out, I just am unsure of how to keep a running total of a team's record. I would do "for line in file:" then on the first line I see that Marshall lost so they would have 1, Ohio State won so they'd have 1 win. It'd go to the next line Kentucky 1 win, Indiana 1 loss, then on the 3rd line, Marshall got a win so they'd have 1 win, but it would have to remember that loss from line 1...

Does this make sense?

Thanks,
 
S

Steven D'Aprano

Marshall,24,Ohio State,48,
Kentucky,14,Indiana,10,
Marshall,10,Indiana,7,
Ohio State,28,Kentucky,10

That's just a quick example, I can handle seperating the data in the
lines, figuring it all out, I just am unsure of how to keep a running
total of a team's record. I would do "for line in file:" then on the
first line I see that Marshall lost so they would have 1, Ohio State won
so they'd have 1 win. It'd go to the next line Kentucky 1 win, Indiana 1
loss, then on the 3rd line, Marshall got a win so they'd have 1 win, but
it would have to remember that loss from line 1...

There are many ways to do this. Here's one: we keep three sets of data,
wins, losses and ties.

wins = {}
losses = {}
ties = {}
for line in open("datafile.txt"):
line = line.strip() # get rid of leading and trailing whitespace
line = line.rstrip(',') # and any trailing comma
teamA, scoreA, teamB, scoreB = line.split(',') # split on commas
teamA = teamA.strip().title() # normalise the case
teamB = teamB.strip().title()
scoreA = int(scoreA)
scoreB = int(scoreB)
if scoreA == scoreB:
# Handle a draw.
ties[teamA] = ties.get(teamA, 0) + 1
ties[teamB] = ties.get(teamB, 0) + 1
else:
if scoreA > scoreB:
winner = teamA
loser = teamB
else:
winner = teamB
loser = teamA
wins[winner] = wins.get(winner, 0) + 1
losses[loser] = losses.get(loser, 0) + 1


Once you've done that, you can check the win/loss score of any team:

name = 'Marshall'
w = wins.get(name, 0)
l = losses.get(name, 0)
d = ties.get(name, 0)
total = w+l+d
print(
"Team %s played %d games, won %d, lost %d and tied %d."
% (name, total, w, l, d)
)


If you want to store these results permanently, you need to write them
out to file. You can roll your own, but a simpler way might be to use one
of the pickle, json, csv or plistlib modules to do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top