简体   繁体   English

检查列表中是否存在字符串会给出 TypeError: argument of type 'float' is not iterable

[英]checking string presence in list gives TypeError: argument of type 'float' is not iterable

I am running into a TypeError when checking a strings' presence in a list:在检查字符串在列表中的存在时,我遇到了 TypeError:

I checked similar issues such as the one in this thread and this one but i couldn't find a solution there.我检查了类似的问题,例如这个线程中的那个和这个,但我在那里找不到解决方案。

I expect the code to calculate (one iteration of) the page rank of all documents inside "pagerank.txt , which looks like this this , but it runs into an error.我希望代码计算(一次迭代) "pagerank.txt中所有文档的页面排名,看起来像这样,但它会遇到错误。

My full code:我的完整代码:

def calc_pageranks(pagerank_file = "pagerank.txt", damping=0.9):
    pagerankScores = {}
    pagerankData = {} #dict of what files a file (the dict key) points to 
    with open(pagerank_file, "r") as f:
        for row in f:
            row = row.split()
            pagerankScores.update({row[0]:1}) #set starting pagerank for documents
            if len(row) == 1:
                pagerankData.update({row[0]:None})
            else:
                pagerankData.update({row[0]:row[1:]}) #pagerank graph
        for docName in pagerankData:
            pagerank = pagerankScores[docName]
            temp = 0
            for val in pagerankData.values():
                if val == None:
                    pass
                else:
                    if docName in val:
                        temp += pagerank / len(val)
                pagerank = (1 - damping) + damping * temp
                pagerankData.update({docName:pagerank})

    return pagerankScores, pagerankData

docName and val look like this: doc1.txt <class 'str'> ['doc2.txt', 'doc8.txt'] <class 'list'> docNameval如下所示: doc1.txt <class 'str'> ['doc2.txt', 'doc8.txt'] <class 'list'>

Full error message:完整的错误信息:

/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 /Users/MacSuperior/Desktop/Coding/IR_system/search_engine.py
Traceback (most recent call last):
  File "/Users/MacSuperior/Desktop/Coding/IR_system/search_engine.py", line 99, in <module>
    print(calc_pageranks())
  File "/Users/MacSuperior/Desktop/Coding/IR_system/search_engine.py", line 92, in calc_pageranks
    if docName in val:
TypeError: argument of type 'float' is not iterable

Pagerank.txt:网页排名.txt:

doc1.txt doc2.txt doc8.txt
doc2.txt doc1.txt doc2.txt doc9.txt
doc3.txt doc4.txt
doc4.txt  doc1.txt doc10.txt
doc5.txt doc6.txt
doc6.txt
doc7.txt doc1.txt
doc8.txt doc9.txt doc10.txt
doc9.txt doc10.txt
doc10.txt doc9.txt

Here's a couple of ideas to refactor to help eliminate typos.这里有一些重构的想法,以帮助消除拼写错误。

def calc_pageranks(pagerank_file = "pagerank.txt", damping=0.9):
    pagerankScores = {}
    pagerankData = {} 
    
    with open(pagerank_file, "r") as f:
        for row in f:
            key, *values = row.split()
            pagerankData[key]  = values
        
        for docName in pagerankData:
            pagerank = (1-damping) + damping * sum((docName in value)/len(value) for value in pagerankData.values() if value)
            pagerankScores[docName] = pagerank
    
    return pagerankScores, pagerankData

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

相关问题 “float”类型的参数不可迭代 - TypeError - argument of type 'float' is not iterable - TypeError 类型错误:“float”类型的参数不可迭代 - TypeError: argument of type 'float' is not iterable Python Lambda:TypeError:“ float”类型的参数不可迭代 - Python lambda: TypeError: argument of type 'float' is not iterable TypeError:“NoneType”类型的参数在列表理解中不可迭代 - TypeError: Argument of type 'NoneType' is not iterable in List comprehension Pandas.DataFrame.Apply-TypeError:类型&#39;float&#39;的参数不可迭代 - Pandas.DataFrame.Apply - TypeError: argument of type 'float' is not iterable TypeError:在 dataframe 中使用 lambda function 时,“float”类型的参数不可迭代 - TypeError: argument of type 'float' is not iterable when using lambda function in dataframe TypeError:'float' 类型的参数不可迭代 - 重塑 csv 文件 - TypeError: argument of type 'float' is not iterable - reshape csv file Python - 'float' 类型的参数不可迭代 - Python - argument of type 'float' is not iterable TypeError:float() 参数必须是字符串或数字,而不是“类型” - TypeError: float() argument must be a string or a number, not 'type' 返回列表为 integer 给出 TypeError: 'type' object is not iterable - Returning list as integer gives TypeError: 'type' object is not iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM