enumerate in python
<p>Say,</p>

<p><code>term='asdf'; InvertedIndex = {}; InvertedIndex[term] = [1,2,2,2,4,5,6,6,6,6,7]</code>.</p>

<p>Now we have this  function which counts no. of occurances of any item. This is the function I've having a problem with.</p>

<pre><code>def TF(term, doc):
    idx = InvertedIndex[term].index(doc)
    return next(i  for i, item in enumerate(InvertedIndex[term][idx:])
                if item != doc)
</code></pre>

<p>It is giving 1 for  <code>TF(term, 1)</code>, 3 for <code>TF(term, 2)</code>,1 for  <code>TF(term, 4)</code>. Fine so far. </p>

<p>But it is giving <strong>StopIteration</strong> error for <code>TF(term, 7)</code>. It is also giving same error if I had <code>InvertedIndex[term] = [7]</code> and called <code>TF(term, 7)</code>. How to fix it?</p>

<p><strong>Edit:</strong>
Clarification about aim of the function. that function is supposed to count no. of occurances of an item. Considering the used example TF(term, 2) must return 3 because it occured 3 times in InvertedIndex[term] </p>

<p><strong>Solution:</strong></p>

<pre><code>def TF(term, doc):
    return InvertedIndex[term].count(doc)
</code></pre>
