Art*_*uro 0 python for-loop python-2.6
我试图运行下面的代码,当我运行python tfidf.py(Python 2.6.9)时,我SyntaxError: invalid syntax在下面的行中得到错误,指向for语句.我究竟做错了什么?
def produceVector(blob, bloblist):
##### SYNTAXERROR: invalid syntax in the "for" in the line below #####
scores = {word: tfidf(word, blob, bloblist) for word in blob.words}
return scores
def tf(word, blob):
return blob.words.count(word) / len(blob.words)
def n_containing(word, bloblist):
return sum(1 for blob in bloblist if word in blob)
def idf(word, bloblist):
return math.log(len(bloblist) / (1 + n_containing(word, bloblist)))
def tfidf(word, blob, bloblist):
return tf(word, blob) * idf(word, bloblist)
Run Code Online (Sandbox Code Playgroud)