Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27894 > unrolled thread

Computing win/loss records in Python

Started byChristopher McComas <mccomas.chris@gmail.com>
First post2012-08-25 22:20 -0400
Last post2012-08-26 14:33 +1000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Computing win/loss records in Python Christopher McComas <mccomas.chris@gmail.com> - 2012-08-25 22:20 -0400
    Re: Computing win/loss records in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-26 03:38 +0000
    Re: Computing win/loss records in Python Ben Finney <ben+python@benfinney.id.au> - 2012-08-26 14:33 +1000

#27894 — Computing win/loss records in Python

FromChristopher McComas <mccomas.chris@gmail.com>
Date2012-08-25 22:20 -0400
SubjectComputing win/loss records in Python
Message-ID<mailman.3821.1345947609.4697.python-list@python.org>
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,

[toc] | [next] | [standalone]


#27899

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-08-26 03:38 +0000
Message-ID<50399a27$0$6574$c3e8da3$5496439d@news.astraweb.com>
In reply to#27894
On Sat, 25 Aug 2012 22:20:05 -0400, Christopher McComas wrote:

> 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.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#27901

FromBen Finney <ben+python@benfinney.id.au>
Date2012-08-26 14:33 +1000
Message-ID<87fw7ae0hf.fsf@benfinney.id.au>
In reply to#27894
Christopher McComas <mccomas.chris@gmail.com> writes:

> 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.

It seems that you already have a PostgreSQL database storing this data.

> I'm trying to get away from Django for this project

That existing database can be accessed without Django. You could talk
directly using the ‘psycopg2’ library, but you don't have to go that
far.

I would recommend you use SQLAlchemy as a good and flexible way to
access existing databases (or make new ones) in a Pythonic manner
<URL:http://www.sqlalchemy.org/>. If you are using a free-software
operating system, you will likely already have packages available to
install SQLAlchemy from your operating system's package repositories.

-- 
 \         “True greatness is measured by how much freedom you give to |
  `\      others, not by how much you can coerce others to do what you |
_o__)                                               want.” —Larry Wall |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web