if you're open to other libraries, this could be done extremely fast in numpy. <div><br></div><div>On my machine summing that whole array takes 75ms.<br><br><div class="gmail_quote">On Thu, Jan 28, 2010 at 8:00 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@remove-this-cybersource.com.au">steve@remove-this-cybersource.com.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Thu, 28 Jan 2010 15:52:14 -0800, elsa wrote:<br>
<br>
> Now, what I need to do is randomly choose one myList[i], however the<br>
> distribution of my random choice needs to be proportional to the values<br>
> of myList[i][0]. So, for this list above, I'd have a much higher chance<br>
> of choosing myList[0] than myList[1].<br>
><br>
> Here is how I'm doing it at the moment:<br>
><br>
> def chooseI(myList):<br>
> mySum=0<br>
> choice = random.choice(range(1,sum([i[0] for i in myList])+1))<br>
> for i in range(len(myList)):<br>
> mySum+=myList[i][0]<br>
> if mySum>=choice:<br>
> return i<br>
> break<br>
<br>
</div>This isn't related to your problem, but you don't need the break after<br>
the return -- the return will leave the loop and the function, and the<br>
break will never be reached.<br>
<br>
You could probably speed the code up a little by changing all the calls<br>
to range into xrange. range actually generates a list of integers, which<br>
is time consuming, while xrange is a lazy generator which just produces<br>
each integer one at a time. (You can ignore this if you are using Python<br>
3.0 or 3.1.)<br>
<br>
Another small optimization you could use is to use a generator expression<br>
instead of a list comprehension inside the call to sum. That should<br>
generate the sum lazily, instead of calculating a giant list first and<br>
then adding it up.<br>
<br>
But I'd try something like this:<br>
<br>
# Untested.<br>
def chooseI(myList):<br>
total = sum(i[0] for i in myList)<br>
mySum = 0<br>
choice = random.randrange(total)<br>
for i in myList:<br>
mySum += i[0]<br>
<div class="im"> if mySum >= choice:<br>
return i<br>
<br>
<br>
<br>
</div>--<br>
<font color="#888888">Steven<br>
</font><div><div></div><div class="h5">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br></div>