I am trying to find the count of words that occured in a file. I have a text file (TEST.txt
) the content of the file is as follows:
ashwin programmer india
amith programmer india
The result I expect is:
{ 'ashwin':1, 'programmer ':2,'india':2, 'amith ':1}
The code I am using is:
for line in open(TEST.txt,'r'):
word = Counter(line.split())
print word
The result I get is:
Counter({'ashwin': 1, 'programmer': 1,'india':1})
Counter({'amith': 1, 'programmer': 1,'india':1})
Can any one please help me? Thanks in advance .
Python Code:def word_count(str): counts = dict() words = str. split() for word in words: if word in counts: counts[word] += 1 else: counts[word] = 1 return counts print( word_count('the quick brown fox jumps over the lazy dog. '))
Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.
Approach: First, we split the string by spaces in a. Then, take a variable count = 0 and in every true condition we increment the count by 1. Now run a loop at 0 to length of string and check if our string is equal to the word.
You're iterating over every line and calling Counter each time. You want Counter to run over the entire file. Try:
from collections import Counter
with open("TEST.txt", "r") as f:
# Used file context read and save into contents
contents = f.read().split()
print Counter(contents)
Use the update
method of Counter. Example:
from collections import Counter
data = '''\
ashwin programmer india
amith programmer india'''
c = Counter()
for line in data.splitlines():
c.update(line.split())
print(c)
Output:
Counter({'india': 2, 'programmer': 2, 'amith': 1, 'ashwin': 1})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With