Re: Trouble with list comprehension

2008-04-09 Thread Steve Holden
Shane Lillie wrote: > I've got a bit of code that looks like this: > > for i in xrange(1000): > # shuffle the doors > doors = [ 'G', 'C', 'G' ] > random.shuffle(doors) > > # save the doors that have goats (by index) > goats = [ x for x in range(2) if doors[x] == 'G' ] > > but

Re: Trouble with list comprehension

2008-04-09 Thread Gary Herron
Shane Lillie wrote: > I've got a bit of code that looks like this: > > for i in xrange(1000): > # shuffle the doors > doors = [ 'G', 'C', 'G' ] > random.shuffle(doors) > > # save the doors that have goats (by index) > goats = [ x for x in range(2) if doors[x] == 'G' ] > Usin

Re: Trouble with list comprehension

2008-04-09 Thread Jeffrey Froman
Shane Lillie wrote: > goats = [ x for x in range(2) if doors[x] == 'G' ] > > but for some reason the list comprehension is not always returning a > list with 2 elements in it (sometimes it will be just 1 element). The problem here is with your usage of the range() function. You provide an endpoi

Trouble with list comprehension

2008-04-09 Thread Shane Lillie
I've got a bit of code that looks like this: for i in xrange(1000): # shuffle the doors doors = [ 'G', 'C', 'G' ] random.shuffle(doors) # save the doors that have goats (by index) goats = [ x for x in range(2) if doors[x] == 'G' ] but for some reason the list comprehension is