title: How to compare word frequencies from two text files?
body: <p>How to compare word frequencies from two text files in python? For example, if a word contains in file1 and file2 both then it should be written only once but not adding their frequencies while comparing, it should be {'The': 3,5}. Here 3 is the frequency in file1 and 5 is frequency in file2. And if some words only exist in one file but not both then for that file there should be 0. Please Help
  Here is what I have done so far:</p>
  
  <pre><code>import operator
  f1=open('file1.txt','r') #file 1
  f2=open('file2.txt','r') #file 2
  
  wordlist=[]
  wordlist2=[]
  for line in f1:
      for word in line.split():
          wordlist.append(word)
  
  for line in f2:
      for word in line.split():
          wordlist2.append(word)
  
  worddictionary = {}
  for word in wordlist:
      if word in worddictionary:
          worddictionary[word] += 1
      else:
          worddictionary[word] = 1
  
  worddictionary2 = {}
  for word in wordlist2:
      if word in worddictionary2:
          worddictionary2[word] += 1
      else:
          worddictionary2[word] = 1
  
  print(worddictionary)
  print(worddictionary2)
  </code></pre>