title: Is there an alternative to `difflib.get_close_matches()` that returns indexes (list positions) instead of a str list?
body: <p>I want to use something like <a href="https://docs.python.org/3/library/difflib.html#difflib.get_close_matches" rel="noreferrer"><code>difflib.get_close_matches</code></a> but <strong>instead of the most similar strings, I would like to obtain the indexes</strong> (i.e. position in the list). </p>
  
  <p>The indexes of the list are more flexible because one can relate the index to other data structures (related to the matched string).</p>
  
  <p>For example, instead of:</p>
  
  <pre><code>&gt;&gt;&gt; words = ['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo', 'question', 'format']
  &gt;&gt;&gt; difflib.get_close_matches('Hello', words)
  ['hello', 'hallo', 'Hallo']
  </code></pre>
  
  <p>I would like:</p>
  
  <pre><code>&gt;&gt;&gt; difflib.get_close_matches('Hello', words)
  [0, 1, 6] 
  </code></pre>
  
  <p>There doesn't seem to exist a parameter to obtain this result, is there an alternative to <code>difflib.get_close_matches()</code> that returns the indexes?</p>
  
  <hr>
  
  <h2>My research towards an alternative</h2>
  
  <p>I know I could use <code>difflib.SequenceMatcher</code>, and then compare the strings one-to-one with <code>ratio</code> (or <code>quick_ratio</code>). However, I am afraid that this would be very inefficient, because:</p>
  
  <ol>
  <li><p>I would have to create thousands of SequenceMatcher objects and compare them (I am expecting that <code>get_close_matches</code> avoid the use of the class):</p>
  
  <p><strong>EDIT</strong>: False. I checked the <a href="https://github.com/python/cpython/blob/master/Lib/difflib.py#L689" rel="noreferrer">source code of <code>get_close_matches</code></a>, it actually uses <code>SequenceMatcher</code>.</p></li>
  <li><p>there is no cutoff (I am guessing that there is an optimization that avoids the calculation of the ratio for all the string)</p>
  
  <p><strong>EDIT</strong>: Partially False. The code is <code>get_close_matches</code> does not have any major optimizations, except it uses <a href="https://github.com/python/cpython/blob/master/Lib/difflib.py#L726-L729" rel="noreferrer"><code>real_quick_ratio</code>, <code>quick_ratio</code> and <code>ratio</code> alltogether</a>. In any case I can easily copy the optimization into my own function. Also I didn't consider that the SequenceMatcher has methods to set the sequences: <code>set_seq1</code>, <code>set_seq2</code>, so at least I won't have to create an object each time.</p></li>
  <li><p>as far as I understand, all python libraries are C compiled and this would increase performance.</p>
  
  <p><strong>EDIT</strong>: I am quite sure this is the case. The function is in the folder called cpython. </p>
  
  <p><strong>EDIT</strong>: There is a small difference (p-value is 0.030198) between executing directly from difflib and copy <a href="https://github.com/python/cpython/blob/master/Lib/difflib.py#L689" rel="noreferrer">the function</a> in a file mydifflib.py. </p>
  
  <pre><code>ipdb&gt; timeit.repeat("gcm('hello', _vals)", setup="from difflib import get_close_matches as gcm; _vals=['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo', 'question', 'format']", number=100000, repeat=10)
  [13.230449825001415, 13.126462900007027, 12.965455356999882, 12.955717618009658, 13.066136312991148, 12.935014379996574, 13.082025538009475, 12.943519036009093, 13.149949093989562, 12.970130036002956]
  
  ipdb&gt; timeit.repeat("gcm('hello', _vals)", setup="from mydifflib import get_close_matches as gcm; _vals=['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo', 'question', 'format']", number=100000, repeat=10)
  [13.363269686000422, 13.087718107010005, 13.112324478992377, 13.358293497993145, 13.283965317998081, 13.056695280989516, 13.021098569995956, 13.04310674899898, 13.024205000008806, 13.152750282009947]
  </code></pre></li>
  </ol>
  
  <p>Nevertheless it is not nearly as bad as I expected, I think I will proceed unless anybody know another library or alternative.</p>