简体   繁体   中英

enumerate in python

Say,

term='asdf'; InvertedIndex = {}; InvertedIndex[term] = [1,2,2,2,4,5,6,6,6,6,7] term='asdf'; InvertedIndex = {}; InvertedIndex[term] = [1,2,2,2,4,5,6,6,6,6,7] .

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

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

It is giving 1 for TF(term, 1) , 3 for TF(term, 2) ,1 for TF(term, 4) . Fine so far.

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

Edit: 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]

Solution:

def TF(term, doc):
    return InvertedIndex[term].count(doc)

I feel like I wrote that loop on another answer but the correct answer for what you want to do is InvertedIndex[term].count(doc)

This will count the number of times that doc occurs in the list.

At the language-level, your problem is that you're calling 'next' on a sequence, and when the sequence is empty it raises StopIteration.

Otherwise, it's not clear how to help you, since it's not obvious what the function you've written is supposed to do. You may want something like this:

def uniq_docs(inverted_index):
    last = None
    for i, doc in enumerate(inverted_index):
        if doc != last:
            yield i, doc
            last = doc

and where you're currently calling TF, use something like:

for index, doc in uniq_docs(InvertedIndex[term]):
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM