0

i want to calculate frequency of each unique word from a given list. for eg. Input list = ['kasol', 'kasol', 'manali', 'delhi', 'delhi', 'manali, 'kasol']

Output = kasol - 3 manali - 2 delhi - 2

i tried

def frequency(a, x):
    count = 0


for i in a:
    if i == x: count += 1
return count

but it is not working.

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76

1 Answers1

0

Use a counter:

L = ['elt1', 'elt1', 'elt2', 'elt2', 'elt3', 'elt4', 'elt5']
from collections import Counter
Counter(L)
Out: Counter({'elt1': 2, 'elt2': 2, 'elt3': 1, 'elt4': 1, 'elt5': 1})
Mathieu
  • 5,410
  • 6
  • 28
  • 55