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.