Return Most Common Words In A Website, Such That Word Count >5
I am new to python. I have a simple program to find the number of times a word has been used in a website. opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'Mozi
Solution 1:
How can i update the script so that the word included, has atleast 5 word count.
You can filter the Counter as follows: filter(lambda x: x[1] > 5, word_counts.iteritems())
filter()
takes a function and an iterable, applies the function to each element of the iterable, and only includes that item in the output if the function returned True
. iteritems()
returns a generator which yields key, value pairs over a dictionary.
how can i arrange the top 5 most common words, into say word1, word2, word3.... etc.
There is a most_common(n)
Counter function. See http://docs.python.org/2/library/collections.html
Solution 2:
Try:
print word_counts.most_common(5)
Post a Comment for "Return Most Common Words In A Website, Such That Word Count >5"