"""Count words."""
def count_words(s, n):
"""Return the n most frequently occuring words in s."""
from operator import itemgetter, attrgetter
# TODO: Count the number of occurences of each word in s
strl_ist = s.replace('\n', ' ').lower().split(' ')
count_dict = {}
for str in strl_ist:
if str in count_dict.keys():
count_dict[str] = count_dict[str] + 1
else:
count_dict[str] = 1
# TODO: Sort the occurences in descending order (alphabetically in case of ties)
count_list=sorted(count_dict.iteritems(),key=itemgetter(1),reverse=True)
top_n = count_list[0:n]
# TODO: Return the top n most frequent words.
return top_n
def test_run():
"""Test count_words() with some inputs."""
print count_words("cat bat mat cat bat cat", 3)
print count_words("betty bought a bit of butter but the butter was bitter", 3)
if __name__ == '__main__':
test_run()
网友评论