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 :(得分:0)
以下代码可以使用,不会给您错误。您尝试将0分配给dict对象,而不是将0添加到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)