How to count the frequency of the words in the file in Python

0 votes

Hi Guys.

I have one file. I want to find the frequency of repeated words in that file. Can anyone tell me how can I do this with the help of Python?

Jun 26, 2020 in Python by akhtar
• 38,260 points
2,987 views

1 answer to this question.

0 votes

Hi@akhtar,

You can use the Counter module to do this task. I have attached one example below for your reference.

from collections import Counter
with open(r'pathinput2.txt') as f:
               p = Counter(f.read().split())
               print(p)
Counter({'and': 3, 'Python': 3, 'that': 2, 'a': 2, 'programming': 2, 'code': 1, '1991,': 1, 'is': 1, 'programming.': 1, 'dynamic': 1, 'an': 1, 'design': 1, 'in': 1, 'high-level': 1, 'management.': 1, 'features': 1, 'readability,': 1, 'van': 1, 'both': 1, 'for': 1, 'Rossum': 1, 'system': 1, 'provides': 1, 'memory': 1, 'has': 1, 'type': 1, 'enable': 1, 'Created': 1, 'philosophy': 1, 'constructs': 1, 'emphasizes': 1, 'general-purpose': 1, 'notably': 1, 'released': 1, 'significant': 1, 'Guido': 1, 'using': 1, 'interpreted': 1, 'by': 1, 'on': 1, 'language': 1, 'whitespace.': 1, 'clear': 1, 'It': 1, 'large': 1, 'small': 1, 'automatic': 1, 'scales.': 1, 'first': 1})

I hope this will help you.

answered Jun 26, 2020 by MD
• 95,460 points

Related Questions In Python

0 votes
1 answer

How to find the value of a row in a csv file in python?

If you want to find the value ...READ MORE

answered May 20, 2019 in Python by Sanam
19,208 views
0 votes
1 answer

What is the Python logic to count the number of capital letters in a file?

Hi, You can use this piece of code, ...READ MORE

answered Jun 26, 2020 in Python by Roshni
• 10,480 points
2,131 views
0 votes
1 answer

How to find the value of a row in a csv file in python and print column and row of that value ?

Hello @Khanhh , Use panda to find the value of ...READ MORE

answered Oct 15, 2020 in Python by Niroj
• 82,840 points
6,758 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,422 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,042 views
0 votes
1 answer

How to split a large sample of text into words in Python?

Hi@akhtar, You can use nltk module to split your text ...READ MORE

answered Jun 26, 2020 in Python by MD
• 95,460 points
1,329 views
0 votes
1 answer

How to remove a file using the OS module in Python?

Hi@akhtar, In Python, you can use the os. remove() method to remove ...READ MORE

answered Oct 18, 2020 in Python by MD
• 95,460 points
1,126 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP