Python语句中的语法无效

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)

Ign*_*ams 5

这不是一个声明,这是一个词典理解.这只是在2.7中引入的.生成一个可迭代的2元组,并将其传递给dict()构造函数.