Question:
Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.
Program:
1 2 3 4 5 6 7 8 9 10 11 |
freq = {} # frequency of words in text line = raw_input("Enter Text:") for word in line.split(): freq[word] = freq.get(word,0)+1 words = freq.keys() words.sort() print "Frequency chart" for w in words: print "%s:%d" % (w,freq[w]) |
Explanation:
The sort() method sorts the elements of a given list in a specific order – Ascending or Descending.
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter Text:Hey There! Hello and hello and welcome to GlobalSQA Welcome Frequency chart GlobalSQA:1 Hello:1 Hey:1 There!:1 Welcome:1 and:2 hello:1 to:1 welcome:1 |
Leave a Reply