python – find the occurrence of the word in a file

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})

Leave a Comment