Python Forum
Python word counter and ranker
Thread Rating:
  • 4 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python word counter and ranker
#2
if word_dict.has_key(word):
# To
if word in word_dict:

word_dict.iteritems()
# To
word_dict.items()

f = open('Alice.txt', 'rU')
# To
f = open('Alice.txt')
A example of more modern Python power Wink
This one is also more accurate,as it remove punctuation(as should be done when count larger text).
from collections import Counter
import re

with open('alice.txt') as f:
    text = f.read().lower()

words = re.findall('\w+', text)
top_10 = Counter(words).most_common(10)
for word,count in top_10:
    print(f'{word:<4} {"-->":^4} {count:>4}')
Output:
the --> 1818 and --> 940 to --> 809 a --> 690 of --> 631 it --> 610 she --> 553 i --> 543 you --> 481 said --> 462
Reply


Messages In This Thread
Python word counter and ranker - by Drone4four - Jan-14-2019, 02:30 AM
RE: Python word counter and ranker - by snippsat - Jan-14-2019, 06:39 AM
RE: Python word counter and ranker - by Drone4four - Jan-15-2019, 02:00 AM
RE: Python word counter and ranker - by snippsat - Jan-15-2019, 02:40 AM
RE: Python word counter and ranker - by Drone4four - Jan-16-2019, 02:25 AM
RE: Python word counter and ranker - by snippsat - Jan-16-2019, 07:04 AM
RE: Python word counter and ranker - by Drone4four - Jan-18-2019, 11:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,595 Aug-12-2021, 04:25 PM
Last Post: palladium
  Python Speech recognition, word by word AceScottie 6 16,182 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  print a word after specific word search evilcode1 8 5,014 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  How to print counter without bracket in python and sort data. phob0s 1 2,843 Jul-25-2019, 05:33 PM
Last Post: ichabod801
  Extending my text file word count ranker and calculator Drone4four 8 5,479 Jan-25-2019, 08:25 AM
Last Post: steve_shambles
  difference between word: and word[:] in for loop zowhair 2 3,770 Mar-03-2018, 07:24 AM
Last Post: zowhair
  python word-docx jon0852 0 3,342 Sep-01-2017, 04:54 AM
Last Post: jon0852

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020