简体   繁体   English

在python中枚举

[英]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] . 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) . 它给1 TF(term, 1) ,3 TF(term, 2) ,1 TF(term, 4) Fine so far. 好到目前为止。

But it is giving StopIteration error for TF(term, 7) . 但它给TF(term, 7) StopIteration错误TF(term, 7) It is also giving same error if I had InvertedIndex[term] = [7] and called TF(term, 7) . 如果我有InvertedIndex[term] = [7]并且称为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] 考虑到使用的示例TF(term,2)必须返回3,因为它在InvertedIndex [term]中出现了3次

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) 我觉得我在另一个答案上写了那个循环,但你想要做的正确答案是InvertedIndex[term].count(doc)

This will count the number of times that doc occurs in the list. 这将计算doc在列表中出现的次数。

At the language-level, your problem is that you're calling 'next' on a sequence, and when the sequence is empty it raises StopIteration. 在语言层面,你的问题是你在一个序列上调用'next',当序列为空时它会引发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: 在你目前正在调用TF的地方,使用以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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