【发布时间】:2018-09-06 13:20:10
【问题描述】:
我从https://stevenloria.com/tf-idf/得到这行代码
scores = {word: tfidf(word, blob, bloblist) for word in blob.words}
tfidf函数在哪里:
import math
from textblob import TextBlob as tb
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.words)
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)
为了更好地理解这个过程,我想把速记循环变成一个看起来很普通的“for”循环。
这是正确的吗?
scores = []
for word in blob.words:
scores.append(tfidf(word, blob, bloblist))
另外,写短形式的for循环有什么好处?
【问题讨论】:
-
原代码行是字典推导,不是列表推导,你可以通过花括号和
key:value对来判断
标签: python