[Tutor] Lists of lists - different sized lists
Kent Johnson
kent37 at tds.net
Fri May 19 14:08:29 CEST 2006
Adam Cripps wrote:
> I'm trying to sort [1] a list of people randomly into groups. My guess
> is that a list of lists will do the trick here. However, the randrange
> fails with an empty range. How can I check for this? I thought that my
> pupils list wouldn't reach zero with the while len(pupils)>0: would
> keep it above 0.
You don't show the pupilslength() function so I will assume it is just
returning len(pupils). One problem is if len(pupils) < size you will
exhaust the pupils list before you fill tmplist. This will happen in the
inner loop so the test for len(pupils) > 0 will not be reached.
One option would be to test len(pupils) > 0 again in the condition for
the inner loop.
BTW you can just test 'if pupils:' rather than 'if len(pupils) > 0:'
Kent
>
> Any help or advice would be appreciated.
>
> TIA
> Adam
>
> [1]
> def groups(pupils, size):
> toplist = []
> tmplist = []
> print len(tmplist)
> while len(pupils)>0:
> print len(pupils)
> while len(tmplist)<=size:
> print "tmplist is ", len (tmplist)
> #How many pupils are we talking about here?
> listlength = pupilslength(pupils)
> rand1 = random.randrange(listlength)
> tmplist.append(pupils[rand1])
> pupils.pop(rand1)
> toplist.append(tmplist)
> tmplist = []
> for i in toplist:
> print i
> print i
More information about the Tutor
mailing list