簡體   English   中英

Python,類型錯誤:'int' 對象不支持項目分配”

[英]Python, TypeError: 'int' object does not support item assignment"

import numpy as np

def computeTF(wordDict, doc):
    tfDict ={}
    for word, count in wordDict.items():
        if count == 0:
            tfDict = 0
        else:
            tfDict[word] = 1 + np.log2(count)
    return tfDict

tfDoc1 = int(computeTF(wordDict1, doc1))

print (tfDoc1)

每當我嘗試運行它時,都會出現錯誤:

'TypeError: 'int' 對象不支持項目分配'。

下面的代碼將工作,不會給你錯誤。 您試圖將 0 分配給 dict 對象,而不是向 dict 項目添加零。

import numpy as np

def computeTF(wordDict, doc):
    tfDict ={}
    for word, count in wordDict.items():
        if count == 0:
            tfDict[word] = 0 #this was wrong
        else:
            tfDict[word] = 1 + np.log2(count)
    return tfDict

tfDoc1 = int(computeTF(wordDict1, doc1))

print (tfDoc1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM