Wednesday, 27 April 2016

Python word count using dictionary

#!/usr/bin/python
import operator
string = "My Name is INDIA , INDIA is My Country , INDIA is the Best Country "
string1=string.split( )
dict={}
print string1
for i in string1:
print i
if i in dict:
dict[i]=dict[i]+1
else:
dict[i]=1

print dict

sorted_x = sorted(dict.items(), key=operator.itemgetter(1))

print "Sorted by Value"

print sorted_x