Sorting distionary by value

David Bolen db3l at fitlinxx.com
Thu Mar 28 15:19:27 EST 2002


philh at comuno.freeserve.co.uk (phil hunt) writes:

> IIRC in Awk you can just say:   freq[word] ++ and it works 
> correctly even when there is no pre-existing index of word in freq.
> 
> IMO it's a pity Python isn't like that.

I'm not so sure.  Doing it that way presumes that in fact you aren't
making an error while trying to access an index (word) that doesn't
exist.

Python requires marginally more effort to cover this case (e.g., you
have to explicitly use a method like .get to include a default value
if you know the key might not yet be present), but that also means
that the code explicitly highlights that the scenario is anticipated
and being handled.

Sure, in a simple example like an increment operation it seems a bit
much, but as a general case, I think it's the smarter and more
maintainable default behavior.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list