"Newbie" questions - "unique" sorting ?

Kim Petersen kp at kyborg.dk
Wed Jun 25 01:58:38 EDT 2003


Bryan wrote:
>>totally skipping the word count should speed it up - and i believe that
>>the approach of dict_words[this_word]=dict_words.get(this_word,0) btw.
>>is a bit faster than doing has_key() or the least of doing:
>>
>>    if this_word not in dict_words.keys() :
>>
>>which aught to be extremely slow on a large dictionary (creating and
>>dropping lists of thousands + doing a O(n) search over it). And that may
>>very well be the culprit of the slow run you see....

> this is how i check if a key is in a dictionary:
> 
> if this_word not in dict_words:
> 
> i believe this doesn't do an O(n) search over it.  it's supposed to constant
> time for every lookup by way of a hash lookup.

Which is probably correct since that would be equivalent to:
  not dict_words.has_key(this_word)

But if you look at the above code mentioned - it doesn't do that - it 
calls .keys() [generating a list] and then looks if the string is in it...

> 
> bryan
> 
> 

-- 
Med Venlig Hilsen / Regards

Kim Petersen - Kyborg A/S (Udvikling)
IT - Innovationshuset
Havneparken 2
7100 Vejle
Tlf. +4576408183 || Fax. +4576408188





More information about the Python-list mailing list