Sorting distionary by value

Steve Holden sholden at holdenweb.com
Fri Mar 29 08:24:28 EST 2002


"phil hunt" <philh at comuno.freeserve.co.uk> wrote in message
news:slrnaa7eem.eau.philh at comuno.freeserve.co.uk...
> On 28 Mar 2002 15:19:27 -0500, David Bolen <db3l at fitlinxx.com> wrote:
> >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.
>
> Correct. IMO "try to do something sensible" should be the default.
>
So you'd like Python to operate in DWIM (do what I mean) mode? Perhaps you'd
also like the development of psychic wireless networking, which knows what
you are going to say so you never need to say it, and all programs simply
become a sinlge statement:

    solve this problem

> If the programmer *wants* to do it the other way round, that is easy
> enough to code:
>
>    if word not in fred:
>       raise SomeException
>    fred[word] ++
>
> But 9 times out of 10, you'll want to use a default of 0.
>
9 times out of 10 a language system that makes guesses about what I want
will be wrong. If I want a system that "makes guesses" then Perl is
available.

> >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.
>
> Having to explicitly state things like this is a sign of low
> levelnmess in a lang. Next you'll be arguing that explicit storage
> management is a good thing.... :-)
>
Explicit storage management is a good thing if you happen to need close
control over storage. But having to be explicit about the semantic of your
problem (rather than the details of using a computer to solve a problem by
manipulating information models) is a sign a maturity, not of
"low-levelness". I guess

    solve this problem

would be high-level in your book? In mine it simply moves the onus of
providing semantic detail on to the description of what "this problem" is.
The problems that take most fixing are the ones where the language system
acts on some unstated assumption which contradicts the assumptions of the
programmer.

> >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.
>
> I have never had maintainability problems with the other way round.
>
You're lucky.

regards
 Steve







More information about the Python-list mailing list