sorting a dictionary

Peter Abel p-abel at t-online.de
Wed Feb 5 23:31:10 CET 2003


Alex Martelli <aleax at aleax.it> wrote in message news:<OQM%9.128856$0v.3661239 at news1.tin.it>...
>                             (may compare keys thus crash
> when one of equal values has a complex key, see my reply
> to A. Bennett) <snip> 

In deed I could find this:
>>> d={'a': 41, 'b': 3, 'd': (1+2j)}
>>> max(d.values())
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: cannot compare complex numbers using <, <=, >, >=

But what I do not understand is why the following doesn't crash:
>>> d={'a': 41, 'b': 'X', 'd': (1+2j)}
>>> max(d.values())
'X'

Coming back once again to the basic problem. What about the
following concerning space, speed etc. when we assume
that values are only numbers.
>>> d={'a': 15, 'b': 12, 'd': 9}
>>> d.keys()[d.values().index(max(d.itervalues()))]
'a'
>>> 

I-like-1-liners-Regards
Peter




More information about the Python-list mailing list