Counter for items in lists in lists?
Charlotte Henkle
charlotte at fgm.com
Sat Sep 25 17:47:38 EDT 2004
Ahhhh...thank you all!
Actually, I wanted to be able to count the items in the list to check
my thinking on a problem, and as it turns out, my thinking was
incorrect. So I have a follow up question now ;)
Some background:
I was given a problem by a friend. He is in a tennis group that has 6
members. They play once a week for 10 weeks. They were having
trouble generating a schedule that was random but fair (IE everyone
gets to play a base number of times , and the mod is evenly and
randomly distributed).
I decided that I wanted to abstract as much of the problem as possible
so that it would be reusable for other groups (IE to solve it for
games with N players, Y times over X weeks). And then I decided I
wanted this to be my second program in python, so forgive my
clumsiness ;)
My first pass through was short and simple: Figure out the total
number of games that will be played, and then use something like this:
gamePlayers=random.sample(templist, players_per_game)
to fill up all the game slots. Neat, simple.
The problem I found with this solution was that it didn't give me a
weighted list for remainder. The same person could get in all of the
"extra" games.
So instead I decided to fill games from my list of players by removal
until I had a number of players left less than a full game, and then
have a back-up list. The back up list would act like a queue, but if
you were already in the game, it would take the next guy. If you got
into a game from the back-up list, you were sent to the end of the
line. My 2 lines grew to 50 plus ;)
Sadly, this isn't quite working either. Now that I can print out the
players, I see that generally things work, but every once in a while,
I get an unexpected result. For example, with 6 players, 10 weeks, 2
games per week, I would expect combinations of:
{'a': 13, 'c': 14, 'b': 14, 'e': 13, 'd': 13, 'f': 13}
(4 13s and 2 14s)
But sometimes I get:
{'a': 13, 'c': 14, 'b': 14, 'e': 14, 'd': 12, 'f': 13}
(2 13s, 3 14s and a 12)
That 12 breaks the planned even distribution. :(
I suspect the problem comes from the random pulling in the first part,
but I'm not sure. I also feel that some sections (espcially the
print) don't have a "python-grace", so I would love some suggestions
to make them more...slithery? :)
To make a long story longer, here's the code:
...#! /usr/bin/env python
...
...#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']
...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
... while x!=weeks:
... y=0
... print "Week: ", x+1
... while y<games:
... print "Game ",y+1, " players are ", list[y]
... y+=1
... x+=1
...
...#printing out and counting the final list
...
...printList(number_of_weeks, games_per_week, finishedList)
...print finishedList.count("a")
More information about the Python-list
mailing list