0

I have been working on finding total tf-idf values of each files from a list of files. So far i've calculated tf-idf values of all words in each file (inside for w in words). Now i want to add the tf-idf value of each word which ultimately gives the tf-idf value for a particular file f. I am somewhat new in Python and i am experiencing some problem in doing so . Any suggestion would be highly appreciated. (for python 2.7)

 for f in file_list:
    (some code)
    for w in words:
        (some code)
        tf_idf = tf_value * idf_value 
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
sulav_lfc
  • 772
  • 2
  • 14
  • 34

2 Answers2

0

Accumulate the total:

 total = 0
 for f in file_list:
    (some code)
    for w in words:
        (some code)
        tf_idf = tf_value * idf_value 
        total += tf_idf
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

Create a dict to keep track of each file's total tf_idf

 filewise_tf_idf = {}
 for f in file_list:
    (some code)
    for w in words:
        (some code)
        tf_idf = tf_value * idf_value 
        filewise_tf_idf[f] = filewise_tf_idf.get(f, 0) + tf_idf
Sunny Nanda
  • 2,362
  • 1
  • 16
  • 10