Find next lower value in list of (float) numbers?
<p>How should I write the <code>find_nearest_lower</code> function?</p>

<pre><code>&gt;&gt;&gt; values = [10.1, 10.11, 10.20]
&gt;&gt;&gt; my_value = 10.12
&gt;&gt;&gt; nearest_lower = find_nearest_lower(values, my_value)
&gt;&gt;&gt; nearest_lower
10.11
</code></pre>

<p>This needs to work in Python 2.6 without access to numpy.</p>

<p>You can use <a href="https://docs.python.org/2/library/itertools.html#itertools.dropwhile" rel="nofollow"><code>itertools.dropwhile</code></a>:</p>

<pre><code>&gt;&gt;&gt; from itertools import dropwhile
&gt;&gt;&gt; values = [10.1, 10.11, 10.20]
&gt;&gt;&gt; my_value = 10.12
&gt;&gt;&gt; next(dropwhile(lambda x: x &gt; my_value, sorted(values, reverse=True)))
10.11
</code></pre>

<p>You can also pass a <code>default</code> argument to <a href="https://docs.python.org/2/library/functions.html#next" rel="nofollow"><code>next()</code></a> which will be returned if the iterator is exhausted instead of raising <code>StopIteration</code>.</p>

python python-2.6