Python: sort function breaks in the presence of nan

前端 未结 6 1975
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 07:34

sorted([2, float(\'nan\'), 1]) returns [2, nan, 1]

(At least on Activestate Python 3.1 implementation.)

I understand nan

6条回答
  •  温柔的废话
    2020-11-29 07:52

    The problem is that there's no correct order if the list contains a NAN, since a sequence a1, a2, a3, ..., an is sorted if a1 <= a2 <= a3 <= ... <= an. If any of these a values is a NAN then the sorted property breaks, since for all a, a <= NAN and NAN <= a are both false.

提交回复
热议问题