Trouble with list comprehension

S

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 not always returning a
list with 2 elements in it (sometimes it will be just 1 element). I've
tried changing to a generator as well as using filter() and all 3 give
the same sort of results. It works if I use a loop, but I'd really
like to know what I'm doing wrong here. Everything looks like it
should be working.

Thanks in advance for any suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top