For loop searching takes too long!

Daniel Stutzbach daniel at stutzbachenterprises.com
Thu Jan 28 23:51:16 EST 2010


On Thu, Jan 28, 2010 at 5:52 PM, elsa <kerensaelise at hotmail.com> wrote:

>        choice = random.choice(range(1,sum([i[0] for i in myList])+1))
>

That's the line causing the problem.  It's generating all of the numbers
between 1 and your sum, then picking one.  Try the following instead, which
will pick a number between 1 and your sum without generating each and every
one.

choice = random.randrange(1, sum(i[0] for i in myList)+1)

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100128/1a609307/attachment-0001.html>


More information about the Python-list mailing list