[Tutor] Word count help
Wed, 31 Jan 2001 10:04:10 +0000
Kalle Svensson wrote:
> Shouldn't this be
> if freqs.has_key(word):
> IIRC, dict.keys() and if x in list are both linear time. dict.has_key() is
> constant time. I repeat, IIRC. <wink> And somebody might have optimized it.
Ah, well, I didn't know about that. Thanks for the tip. Actually what you say
makes perfecct sense, since key in dict.keys() is a brute force solution to this
problem, while dict.has_key(key) probably uses the dictionary hashing function to
drop in fast at the (eventual) location of key in the dictionary.
I once implemented a dictionary in fortran (I didn't know then that it is a
common feature in more modern languages), and I also defined search functions
that do the equivalent of dict.has_key(), instead of using the brute force
method. Now that I'm spoiled by the use of language with all the proper tools
built in, I tend to forget about them!