[Tutor] Counting words
Nicole Seitz
nicole.seitz@urz.uni-hd.de
Fri, 5 Apr 2002 13:28:30 +0200
Am Donnerstag, 4. April 2002 21:42 schrieben Sie:
> what is happening is the 'get' method of the dictionary is being called.
> It is looking for the key specified by word and if it is not found it
> returns 0.
>
> This is equivalent to:
>
> if occurences.has_key(word):
> occurences[word] = occurences[word] + 1
> else:
> occurences[word] = 1
Now it's very clear, thanks!
>
> > Second question:
> > "Some" and "some" should be recognized as one word, the same is with
> > "BORING" and "boring". I thought of string.lowercase as a possible
> > solution, but as it doesn't work , I might be wrong. Any idea what to
> > do?
>
> word = string.lower(word)
I am so stupid! I do know this function from string module. I mixed up
lower() and lowercase. I guess one should not program when he or she is
totally overtired.It's much better when your brain's engaged:-)
> ['A', 'typical', 'line', '']
>
> notice the empty last entry. Have you loop skip empty words.
Like this?
for word in reg.split(text):
if word == "": continue
word = string.lower(word)
occurences[word] = occurences.get(word,0)+1
Many thanks.
Nicole