Python program to count distinct words and count frequency of them

PythonServer Side ProgrammingProgramming

Suppose we have a list of words. These words may occur multiple times. We have to show the frequencies of these words and count how many distinct words are there.

So, if the input is like words = ["Book", "Sound", "Language", "Computer", "Book", "Language"], then the output will be (4, '2 1 2 1') because there are four distinct words, the first and third words have occurred twice.

To solve this, we will follow these steps −

  • d:= an OrderedDict to store items based on insert order
  • for each w in words, do
    • if w is in d, then
      • d[w] := d[w] + 1
    • otherwise,
      • d[w] := 1
  • a pair of the size of list of all keys in d and join all values from d into a string then return.

Example

Let us see the following implementation to get better understanding

from collections import OrderedDict

def solve(words):
   d=OrderedDict()
   for w in words:
      if w in d:
         d[w] += 1
      else:
         d[w] = 1
   return len(d.keys()), ' '.join([str(d[k]) for k in d.keys()])

words = ["Book", "Sound", "Language", "Computer", "Book", "Language"]
print(solve(words))

Input

["Book", "Sound", "Language", "Computer", "Book", "Language"]

Output

(4, '2 1 2 1')
raja
Published on 12-Oct-2021 08:02:11
Advertisements