[Python-3000] NaN in sorting

Bruce Leban bruce at leapyear.org
Fri Nov 14 00:09:04 CET 2008


I agree that that function will fix the problem. You can also use it to fix
sorting any mixed types but that's not my point either. My point is that
when you have None mixed in with numbers, sorting doesn't work in a way that
stops you and makes you fix it. When you have nan mixed in, it just fails
randomly.

The sorted function should satisfy the property that sorted[i] < sorted[j]
for all i < j and it doesn't. I realize that it *can't* satisfy it for the
nans because < is always false regardless. But it should at least satisfy it
for the comparable values and it doesn't.

--- Bruce


On Thu, Nov 13, 2008 at 1:01 PM, Jeffrey Yasskin <jyasskin at gmail.com> wrote:

> Be glad you're not programming in C++ then, where trying to sort NaN
> can cause segfaults!
>
> More seriously, I think using the following function as the sort key
> will make sort do what you want:
>
> def SortNoneFirstAndNanLast(x):
>  if x is None:
>    return (1, x)
>  if isnan(x):
>    return (3, x)
>  return (2, x)
>
> No need to modify either sort() or <.
>
> On Thu, Nov 13, 2008 at 11:15 AM, Bruce Leban <bruce at leapyear.org> wrote:
> > I think the behavior of NaN in comparisons is more confusing:
> >>>> sorted([1,nan,2])
> > [1, nan, 2]
> >>>> sorted([2,nan,1])
> > [2, nan, 1]
> >>>> sorted([2,None,1])
> > Traceback (most recent call last):
> >   File "<pyshell#28>", line 1, in <module>
> >     sorted([2,None,1])
> > TypeError: unorderable types: NoneType() < int()
> > At least the third case is clear that I shouldn't have done that. The way
> > nan works, the results of sorting where one of the values is nan is
> > unpredictable and useless.
> > Yes, I know the rules about how NaN values behave in comparisons.
> > Notwithstanding that, sorting could use a different comparison rule
> imposing
> > a total ordering: -inf, ..., inf, nan as some other systems do.
> > --- Bruce
> >
> > On Thu, Nov 13, 2008 at 10:55 AM, Tim Peters <tim.peters at gmail.com>
> wrote:
> >>
> >> [M.-A. Lemburg]
> >> > ...
> >> > So far, I haven't heard a single argument for why not having None
> >> > participate in an ordering scheme is a good strategy to use, except
> >> > that it's pure.
> >>
> >> I've tracked down plenty of program logic errors that would have been
> >> discovered more easily if comparing None to (mostly, but among others)
> >> integers and strings had raised an exception instead of returning a
> >> meaningless true/false result.  Perhaps you haven't.  For those who
> >> have, the attraction to making comparisons with None refuse to return
> >> nonsense silently is both obvious and visceral.
> >>
> >>
> >> > IMHO, practicality beats purity in this special case.
> >>
> >> If hiding program logic errors is practical, sure ;-)
> >>
> >> there-is-no-behavior-no-matter-how-bizarre-someone-won't
> >> come-to-rely-on-ly y'rs  - tim
> >> _______________________________________________
> >> Python-3000 mailing list
> >> Python-3000 at python.org
> >> http://mail.python.org/mailman/listinfo/python-3000
> >> Unsubscribe:
> >> http://mail.python.org/mailman/options/python-3000/bruce%40leapyear.org
> >
> >
> > _______________________________________________
> > Python-3000 mailing list
> > Python-3000 at python.org
> > http://mail.python.org/mailman/listinfo/python-3000
> > Unsubscribe:
> > http://mail.python.org/mailman/options/python-3000/jyasskin%40gmail.com
> >
> >
>
>
>
> --
> Namasté,
> Jeffrey Yasskin
> http://jeffrey.yasskin.info/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-3000/attachments/20081113/16cad849/attachment-0001.htm>


More information about the Python-3000 mailing list