-4

I have this programme here that counts how many times a word was used in my .txt file and displays the top 10 most common words. I am hoping to display this information without the numbers as well though, so just the words. Any help would be greatly appreciated.

def countWordFrequency(Data1):
   myDict = {}
   fh = open(Data1, 'r')
   content = fh.read()
   listWords = content.split(" ")
   for w in listWords:
      if (myDict.has_key(w)):
         myDict[w] = myDict[w] + 1
      else:
         myDict[w] = 1
   fh.close()
   return myDict

freq = countWordFrequency('Data1.txt')
topfreq = sorted(freq.iteritems(), key=lambda x:-x[1])[:10]
for x in topfreq:
    print "{0}: {1}".format(*x)
K-8
  • 99
  • 1
  • 3

1 Answers1

0

How about some list comprehension magic?

f = open('Data1.txt','r')
words = [x for y in [l.split() for l in f.readlines()] for x in y]
print sorted([(w, words.count(w)) for w in set(words)], key = lambda x:x[1], reverse=True)[:10]

I'm sure you could do it in one line if you tried hard enough.