There are mistakes like your last line should be i = i + 1. Also string functions are builtin since version 2.2, module re is not needed.
Here is one way to do this with version 2.5
# count words in a text and show the first ten items
# by decreasing frequency
# sample text for testing
text = """\
My name is Fred Flintstone and I am a famous TV
star. I have as much authority as the Pope, I
just don't have as many people who believe it.
"""
word_freq = {}
word_list = text.split()
for word in word_list:
# word all lower case
word = word.lower()
# strip any trailing period or comma
word = word.rstrip('.,')
# build the dictionary
count = word_freq.get(word, 0)
word_freq[word] = count + 1
# create a list of (freq, word) tuples
freq_list = [(freq, word) for word, freq in word_freq.items()]
# sort the list by the first element in each tuple (default)
freq_list.sort(reverse=True)
for n, tup in enumerate(freq_list):
# print the first ten items
if n < 10:
freq, word = tup
print freq, word
# or
#print word, freq
"""
my output -->
3 i
3 as
2 have
1 who
1 tv
1 the
1 star
1 pope
1 people
1 name
"""
Last edited by ZZucker : Mar 14th, 2008 at 12:17 pm.
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.