<br><br><div class="gmail_quote">On Sat, Mar 20, 2010 at 1:17 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info">steve@pearwood.info</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 Sat, 20 Mar 2010 05:47:45 am James Reynolds wrote:<br>
<br>
> This is a monte-carlo simulation.<br>
><br>
> The simulation measures the expiration of something and those<br>
> somethings fall into bins that are not evenly dispersed. These bins<br>
> are stored in the nx list mentioned previously.<br>
><br>
> So let's say you have the bins, a, b,c,d,e,f and you have the value z<br>
> from the sample list where z >b and <= a. In this case, it should<br>
> return the index value at position (a).<br>
<br>
</div>I'm not sure I understand completely. An example might help. I *think*<br>
you have a list like this:<br>
<br>
nx = [10.0, 9.0, 7.0, 3.0, 2.0, 1.0]<br>
<br>
and if you have a value like z = 9.8 you want to return the index 0.<br>
Correct?<br>
<br>
That seems a bit funny. In my experience it is normal to have the bins<br>
in the opposite direction. I suppose it probably doesn't matter that<br>
much, but it does seem a bit unusual.<br>
<br>
If nx is fairly short (say, less than 40 or 50 items), then the fastest<br>
way is probably a linear search, something like this:<br>
<br>
def search_bins(nx, z):<br>
"""Search bins nx for item z.<br>
<br>
>>> bins = [5.0, 4.0, 2.0, 1.0]<br>
>>> search_bins(bins, 1.2)<br>
2<br>
<br>
If z is not in the bins, returns -1:<br>
<br>
>>> search_bins(bins, 5.1)<br>
-1<br>
<br>
"""<br>
for i, value in enumerate(nx):<br>
if z > value:<br>
return i-1<br>
return -1<br>
<br>
<br>
<br>
<br>
--<br>
<font color="#888888">Steven D'Aprano<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br><div><br></div><div>Firstly,</div><div><br></div><div>Thank you to everyone who responded to this thread. I learned some very important lessons, some of which I've been working on over the weekend. I apologize for not responding sooner.</div>
<div><br></div><div>I tinkered around with the aforementioned algorithm and I ended up using the bisect that Emilie showed earlier. I did end up making the nested for loop work earlier using an if statement, but the bisect was much more efficient (.24 seconds running 100K trials), which is a major improvement. I've just started using profiler this weekend, and It's giving me some incite into how the language functions.</div>
<div><br></div><div>On that end, I'm almost done readying "beginning Python: From Novice to Professional" Can anyone recommend anything else for me to read after that?</div><div><br></div><div><br></div><div>
<br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>