def word_count_dict(filename):
file = open(filename, 'rU')
words = [] #list of words in file
dict = {} #returns how many times the word has been used
words = file.read().split()
file.close()
words = [word.lower() for word in words]
words = sorted(words)
for word in words:
dict[word] = words.count(word)
return dict
def word_count_dict(filename):
word_count = {} # Map each word to its count
input_file = open(filename, 'r')
for line in input_file:
words = line.split()
for word in words:
word = word.lower()
# Special case if we're seeing this word for the first time.
if not word in word_count:
word_count[word] = 1
else:
word_count[word] = word_count[word] + 1
input_file.close() # Not strictly required, but good form.
return word_count
def get_trimmed(word):chars = [',','.','!','?','"',"'",':',';','-','`',')']for c in chars:word = word.replace(c,'')return word.lower()def get_words(filename):d = {}for line in open(filename).read().split():word = get_trimmed(line)d.setdefault(word, 0)d[word] += 1return d
--
You received this message because you are subscribed to the Google Groups "Python GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-gcu-for...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Python GCU Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-gcu-forum/JhbJbEOLeg0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-gcu-for...@googlegroups.com.