Newbie: Help Figger Out My Problem

Casey Hawthorne caseyhHAMMER_TIME at istar.ca
Tue Jun 28 20:51:58 EDT 2005


It may be shorter but it keeps the entire list in memory and has to
iterate over the list twice!
Does he/she need the entire list?

import random
heads = 0
flips = 1000000
for i in xrange(flips):
    if random.randint(0,1): heads += 1
print "Heads:%s" % heads
print "Tails:%s" % (flips - heads)


"Devan L" <devlai at gmail.com> wrote:

>import random
>flips = 100
>results = [random.randint(0,1) for i in range(flips)]
>heads = results.count(0)
>tails = results.count(1)
>print "Heads:%s" % heads
>print "Tails:%s" % tails
>I think this is more compact.

--
Regards,
Casey



More information about the Python-list mailing list