<br><br><div class="gmail_quote">On Sat, Mar 20, 2010 at 1:17 PM, Steven D&#39;Aprano <span dir="ltr">&lt;<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>&gt;</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>
&gt; This is a monte-carlo simulation.<br>
&gt;<br>
&gt; The simulation measures the expiration of something and those<br>
&gt; somethings fall into bins that are not evenly dispersed. These bins<br>
&gt; are stored in the nx list mentioned previously.<br>
&gt;<br>
&gt; So let&#39;s say you have the bins, a, b,c,d,e,f and you have the value z<br>
&gt; from the sample list where z &gt;b and &lt;= a. In this case, it should<br>
&gt; return the index value at position (a).<br>
<br>
</div>I&#39;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&#39;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>
    &quot;&quot;&quot;Search bins nx for item z.<br>
<br>
    &gt;&gt;&gt; bins = [5.0, 4.0, 2.0, 1.0]<br>
    &gt;&gt;&gt; search_bins(bins, 1.2)<br>
    2<br>
<br>
    If z is not in the bins, returns -1:<br>
<br>
    &gt;&gt;&gt; search_bins(bins, 5.1)<br>
    -1<br>
<br>
    &quot;&quot;&quot;<br>
    for i, value in enumerate(nx):<br>
        if z &gt; value:<br>
            return i-1<br>
    return -1<br>
<br>
<br>
<br>
<br>
--<br>
<font color="#888888">Steven D&#39;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&#39;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&#39;ve just started using profiler this weekend, and It&#39;s giving me some incite into how the language functions.</div>
<div><br></div><div>On that end, I&#39;m almost done readying &quot;beginning Python: From Novice to Professional&quot; 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>