large dictionary creation takes a LOT of time.

R. C. James Harlow james at wrong.nu
Fri Apr 29 17:26:41 EDT 2005


On Friday 29 April 2005 11:53, Ville Vainio wrote:
> >>>>> "Kent" == Kent Johnson <kent37 at tds.net> writes:
>
>     Kent> if frequency.has_key(word):
>     Kent> frequency[word] += 1
>     Kent> else:
>     Kent> frequency[word] = 1
>
> This is a good place to use 'get' method of dict:
>
> frequency[word] = frequency.get(word,0) + 1

try/except might be fastest of all:

http://gumuz.looze.net/wordpress/index.php/archives/2005/04/28/python-dictionary-speed-optimisation/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050429/af4dffdb/attachment.sig>


More information about the Python-list mailing list