找到一份通过的代码,你们看看 #!/usr/bin/env python import math import random import sys class Solution2(object):     def tfidf(self, line, idf):         tf = {}         tfidf = []         data = []         words = line.split(",")         length = len(words)         for word in words:             tf[word] = tf.get(word, 0) + 1 / length         for word in words:             tfidf.append(str(round(tf.get(word) * idf.get(word), 2)))             data.append("tf:%s,idf:%s" % (tf.get(word), idf.get(word)))         return (tfidf)     def getWordDocs(self, docs):         N = len(docs)         idf = {}         for doc in docs:             words = set(doc.split(","))             for word in words:                 idf[word] = idf.get(word, 0) + 1         for (k, v) in idf.items():             idf[k] = round(math.log(N / v, 2), 2)         return idf if __name__ == "__main__":     solution2 = Solution2()     n = int(sys.stdin.readline().strip())     dd = []     for i in range(0, n):         dd.append(sys.stdin.readline().strip())     idf = solution2.getWordDocs(dd)     for item in dd:         tfidf = solution2.tfidf(item, idf)         print(",".join(tfidf))
点赞 3
牛客网
牛客企业服务