Counter for items in lists in lists?

Charlotte Henkle charlotte at fgm.com
Sun Sep 26 00:22:45 EDT 2004


> To make a long story longer, here's the code:

Whoops...Correction:

...#! /usr/bin/env python
...#Charlotte Henkle
...
...#A program to randomly fill a tennis schedule
...#
...#The original theory looked like this:
...#       gamePlayers=random.sample(templist, players_per_game)
...#       print gamePlayers
...#
...#But that didn't give a weighted list for extra games
...
...import random
...
...#Eventually these will get set dynamically
...number_of_weeks=10
...players=['a', 'b', 'c', 'd', 'e', 'f', 'g']
...games_per_week=2
...players_per_game=4
...games=number_of_weeks*games_per_week
...
...#this will be used to pull off "extra game" players
...backupList=players[:]
...random.shuffle(backupList)
...
...#a templist so we can modify it.
...templist=players[:]
...
...#our finished product:
...finishedList=[]
...
...while len(finishedList)!=games:
...        if len(templist)>=players_per_game:
...                gamePlayers=[]
...                while len(gamePlayers)!=players_per_game:
...                        randomNumber=random.randint(0,
len(templist)-1)
...                        potentialPlayer=templist.pop(randomNumber)
...                        gamePlayers.append(potentialPlayer) 
...                finishedList.append(gamePlayers)
...        else: 
...                gamePlayers=templist
...                print "I am the leftover game players", gamePlayers
...                print "I am the list of backup players", backupList
...                count=0
...                while len(gamePlayers)!=players_per_game:
...                        print "I am a potential player "
...                        potentialPlayer=backupList[count]
...                        print potentialPlayer   
...                        print "checking to see if I'm in the game"
...                        if potentialPlayer not in gamePlayers:
...                                print "I do not think the player is
in the game"
...                                print "I am the back-up list",
backupList
...                               
potentialPlayer=backupList.pop(count)
...                                gamePlayers.append(potentialPlayer)
...                                backupList.append(potentialPlayer)
...                               print "I am the back-up list after
reorder", backupList
...                                print "I am the gamePlayers after
test and insertion", gamePlayers
...                                        
...                        else: 
...                                print "I think that player is in
the game"
...                                count+=1
...                finishedList.append(gamePlayers)
...                templist=players[:]     
...
...#count the list (thank you, Steve!
...http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&threadm=dI85d.47490%24wV.31831%40attbi_s54&prev=/groups%3Fq%3Dcomp.lang.python%26ie%3DUTF-8%26hl%3Den%26btnG%3DGoogle%2BSearch
...
...def count(item):
...     if not isinstance(item, list):
...             return {item:1}
...     counts = {}
...     for i in item:
...             for key, ct in count(i).items():
...                     counts[key] = counts.get(key, 0) + ct
...     return counts
...
...def printList(weeks, games, list):
...        x=0
...        y=0
...        index=0
...        while x!=weeks:
...                print "Week: ", x+1
...                y=0
...                while y<games:
...                        print "Game ",y+1, " players are ",
list[index]
...                        y+=1
...                        index+=1
...                x+=1
...
...#printing out and counting the final list
...
...printList(number_of_weeks, games_per_week, finishedList)
...print count(finishedList)



More information about the Python-list mailing list