[Tutor] files - strings - lists

Kent Johnson kent37 at tds.net
Sat Nov 26 12:56:12 CET 2005


<resending as the original never made it to the list>

Andrzej Kolinski wrote:
> 
>   OK, I made some progress I think. I added a few lines to Kent's script 
> to get closer what I really am after:

Congratulations! See some notes below.

> 
> ==========================================
> lines = open('liga050926.sbk')   # to get the data from a real file
> 
> #lines = iter(data)  # getting data from a string, you don't need this 
> when reading a file
> 
> lines.next()    # skip two headers
> lines.next()
> 
> header = lines.next().split()
> hands = int(header[2])
> rounds = int(header[3])
> boards = hands*rounds
> 
> lines.next()
> 
> 
> allScores = {}  # accumulate scores into a dictionary whose key is the name
> 
> # Now we can process the names and scores in a loop
> try:    # you don't say how you know the end of the names, I just run to 
> the end of data
>     while True:
>         names = lines.next().strip()
>         player1 = names.split()[0]
>         player2 = names.split()[2]

This could be
       names = lines.next().strip().split()
       player1 = names[0]
       player2 = names[2]
or even
       player1, player2 = lines.next().strip().split(' - ')
>        
>         lines.next()    # skip line after name
>         scores = [ int(lines.next().split()[2]) for i in range(rounds) ]
>         tScores = 0
>         for i in scores:
>             iScore = float(i)
>             tScores = tScores + iScore

This could be
 tScores = float(sum(scores))
> 
>         allScores[player1] = tScores/boards
>         allScores[player2] = tScores/boards
>    
> except: # no more lines
>     if lines.next() == '1,1':
>         pass

I'm not sure what the two lines above are doing? It looks like you don't have a good way to detect the end of the data and you just catch some exception...you should figure out a clean way to exit the loop. Maybe when you read the names line you can look for '1,1' and break out of the loop, if that is what always follows the data you care about.

>    
> for player1, tScores in allScores.items():
>     print player1, tScores
> =============================================
> 1.        I singled out the players names.
> 2.        I added the players scores and divided by the number of boards 
> played.
> 3.        The output contents is what I wanted:
> 
> Chrabalowski 0.875
> Kowalski -0.333333333333
> Kolinski 1.29166666667
> Bohossian 1.29166666667
> Stankiewicz -1.16666666667
> Cwir -0.708333333333 ...
> 
> 4.        The next step for me would be to read the data from more, 
> similar files (from 2 to 10) representing different games and generate 
> an average score for each participating player (a player does not 
> necessary plays each game and I would eventually like to calculate 
> averages of best six out of maximum ten games). Tough! How should I 
> start this next step? (I would like to keep both options open:
>         final ranking = (all tScores)/all boards), or
>         final ranking = average(RScores/boards, RScores/boards, 
> RScores/boards, ...)
>                                         game1                       
>  game2                        game3)

I would save more data for each player. Instead of just keeping the average for the player, I would keep a list of pairs of (tScore, boards) for each game. Instead of 
 allScores[player1] = tScores/boards
you could say
 playerScores = allScores.get(player1, [])
 playerScores.append( (tScores, boards) )
 allScores[player1] = playerScores

all of which can be written more tersely (and obscurely) as
 allScores.setdefault(player1, []).append( (tScores, boards) )

Of course you need a loop to read all the files and you will initialize allScores before the loop. Then when you finish with the files you can retrieve the players' stats and process them either way you want.

BTW what game is this? It seems odd that both players get the same score.

> 
> Thanks Kent, Chris and Danny. After many, many months of using or 
> modifying (and using) existing scripts, with your invaluable help I feel 
> I can write script that is original and extremely useful to me!

Glad to hear it!

Kent




More information about the Tutor mailing list