sorting a dictionary
cnetzer at mail.arc.nasa.gov
Thu Feb 6 03:46:15 CET 2003
On Wed, 2003-02-05 at 18:24, Erik Max Francis wrote:
> It's a tip of the hat to the mathematical fact that comparisons between
> complex numbers is undefined.
I think this was understood by Peter Abel (who demonstrated a point
about comparing complex numbers with floats and strings). He ultimately
had a different point (I am assuming).
I believe what he found odd was that, this doesn't throw an exception:
>>> max([41, 'X', (1+2j)])
whereas this does:
>>> max([41, (1+2j), 'X'])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot compare complex numbers using <, <=, >, >=
So the order of the list of values can determine whether max()/min()
will raise a TypeError. And given that the original post was about
sorting dictionary keys, which have no defined ordering, this could
(possibly) bite someone who isn't prepared to catch the exception
(because the issue may not arise in testing, depending on what order the
test data is returned from dict.items())
> I've never understood why this "feature" generates such discord.
Granted. But, things like the above don't help. From a mathematician's
point of view, I'd want max to ALWAYS throw an exception on this data
set (because I'd want to think of the problem as being "find the largest
value in the set of values", where ordering of comparisons doesn't even
come into it. Of course, I probably wouldn't be comparing strings
either. :) )
Bay Area Python Interest Group - http://www.baypiggies.net/
(any opinion expressed is my own and not NASA's or my employer's)
More information about the Python-list