Find Frequency of words and output each word-group and the frequency

Viewed 23

I need help. I have tried this for about 2 weeks and to no avail.

I have strings of words and I want to find the frequency of each word group, print the words (doesn't matter if word appear multiple times), and the total frequency for each word group by each words.

For example, I have words as follows:

'abc'
'abc'
'abc'
'abc'
'xyz'
'xyz'
'tuf'
'pol'
'pol'
'pol'
'pol'
'pol'
'pol'

and need an output as:

'abc', 4
'abc', 4
'abc', 4
'abc', 4
'xyz', 2
'xyz', 2
'tuf', 1
'pol', 6
'pol', 6
'pol', 6
'pol', 6
'pol', 6
'pol', 6

I am using python3 and I have tried this code and it doesn't work as expected:

curr_tk = None                         
tk = None  
count = 0 

for items in data:
    line = items.strip()
    file = line.split(",") 
    tk = file[0]

   if curr_tk == tk:
      count += 1

   else:
      if curr_tk:
         print ('%s , %s' % (curr_tk, count))
      count = 1
      curr_tk = tk

  #print last word
  if curr_tk == tk:
      print ('%s , %s' % (curr_tk,count))

The above code gives me output as:

'abc', 4
'xyz', 2
'tuf', 1
'pol', 6

but that is not what I wanted.

2 Answers

Assuming your data comes from a list, then you can do the following

data = ['abc', 'abc', 'abc', 'abc', 'xyz', 'xyz', 'tuf', 'pol', 'pol', 'pol', 'pol', 'pol', 'pol']

frequency_map = {}
for item in data:
    if item in frequency_map:
        frequency_map[item] += 1
    else:
        frequency_map[item] = 1

for item in data:
    print(f"{item}, {frequency_map[item]}")

And your output will be:

abc, 4
abc, 4
abc, 4
abc, 4
xyz, 2
xyz, 2
tuf, 1
pol, 6
pol, 6
pol, 6
pol, 6
pol, 6
pol, 6

You can apply the same concept to data coming from any source. This is called a frequency list and it is what you are looking for here.

The easiest way would be something like this:

data = ['abc','abc','abc','abc','xyz','xyz','tuf','pol','pol','pol','pol','pol','pol']
for item in data :
    print(item, data.count(item))
Related