Working on a word occurrence count application in a Python 3.2 / Windows environment.
Can anyone please help to tell me why the following isn't working?
from string import punctuation
from operator import itemgetter
N = 100
words = {}
words_gen = (word.strip(punctuation).lower() for line in open("poi_run.txt")
for word in line.split())
for word in words_gen:
words[word] = words.get(word, 0) + 1
top_words = (words.iteritems(), key=itemgetter(1), reverse=True)[:N]
for word, frequency in top_words:
print ("%s %d") % (word, frequency)
The trace back error is:
Message File Name Line Position
Traceback
<module> C:\Users\will\Desktop\word_count.py 13
AttributeError: 'dict' object has no attribute 'iteritems'
Thanks
n.b.
Fully working code:
from string import punctuation
from operator import itemgetter
N = 100
words = {}
words_gen = (word.strip(punctuation).lower() for line in open("poi_run.txt")
for word in line.split())
for word in words_gen:
words[word] = words.get(word, 0) + 1
top_words = sorted(words.items(), key=itemgetter(1), reverse=True)[:N]
for word, frequency in top_words:
print ("%s %d" % (word, frequency))
Thanks again guys
Another simple way to count the number of words in a Python string is to use the regular expressions library, re. The library comes with a function, findall (), which lets you search for different patterns of strings.
Count frequency of words in string in Python Using Count () Count () can be used to count the number of times a word occurs in a string or in other words it is used to tell the frequency of a word in a string. We just need to pass the word in the argument .
' 'Python is an interpreted, high-level, general-purpose programming language' has total words: 8'' Hi. My name is Ashwini ' has total words: 5
split function is quite useful and usually quite generic method to get words out of the list, but this approach fails once we introduce special characters in the list. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
In Python 3, use just items
where you'd previously use iteritems
.
The new items()
returns a dictionary view object that supports iteration as well as len
and in
.
And of course, in top_words = (words.iteritems(), ...
you forgot to call the sorted
function.
Edit: Please see my other answer for a better solution.
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