Here is the code:
file=open("out1.txt","r+")
wordcount={}
for word in file.read().split():
word = word.lower()
if word.isalpha == True:
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
copy = []
for k,v in wordcount.items():
copy.append((v, k))
copy = sorted(copy, reverse=True)
for k in copy:
print '%s: %d' %(k[1], k[0])
Out1.txt
:
hello there I am saying hello world because Bob is here and I am saying hello because John is here
Runs as:
hello: 3
saying: 2
is: 2
here: 2
because: 2
am: 2
I: 2
world: 1
there: 1
and: 1
John: 1
Bob: 1