MATCH function in python?
<p>Is there a way to do an Excel match() function in Python, such that:</p>

<p>In a graph like this  </p>

<p><img src="http://i.stack.imgur.com/O3oiF.png" alt="K-Means Elbow Method results">...</p>

<p>...wherein I cut-off at y = 90, I would like to print which corresponding x value is the closest.</p>

<p>Based on my investigation the value/answer should be 4, but how can I possibly print or store this in a variable? </p>

<pre><code>In: print(bss/tss*100)

Out: [  1.21976032e-14   7.42743185e+01   8.51440985e+01   9.21584826e+01
        9.59771981e+01   9.74117561e+01   9.82980987e+01   9.90505760e+01
        9.92982678e+01   9.94756800e+01   9.96396123e+01   9.97126077e+01
        9.97593424e+01   9.98030600e+01   9.98344280e+01   9.98692896e+01
        9.98840717e+01   9.99020097e+01   9.99142963e+01]
</code></pre>

<p>You can also beat it to death with this simple-minded function.
It finds the first value at least as large as the target value, checks the previous value, and returns the position (1-based, not 0-based) of the closer value.</p>

<pre><code>def match (table, target):
    for over in range(len(table)):
        if table[over] &gt;= target:
            break

    # over is the index of the first vale at least as large as the target
    # return the position (index+1) of the nearer value
    return over+1 if 2*target &gt; table[over-1] + table[over] \
           else over


ss_table = [
    1.21976032e-14,  7.42743185e+01,  8.51440985e+01,  9.21584826e+01,
    9.59771981e+01,  9.74117561e+01,  9.82980987e+01,  9.90505760e+01,
    9.92982678e+01,  9.94756800e+01,  9.96396123e+01,  9.97126077e+01,
    9.97593424e+01,  9.98030600e+01,  9.98344280e+01,  9.98692896e+01,
    9.98840717e+01,  9.99020097e+01,  9.99142963e+01
]

print match(ss_table, 87)
print match(ss_table, 90)
</code></pre>

<p>For your example, this returns 3 and 4, as desired.</p>

python match cluster-analysis k-means