How can I get 2.x-like sorting behaviour in Python 3.x?

后端 未结 10 842
陌清茗
陌清茗 2020-11-27 06:50

I\'m trying to replicate (and if possible improve on) Python 2.x\'s sorting behaviour in 3.x, so that mutually orderable types like int, float etc.

10条回答
  •  攒了一身酷
    2020-11-27 07:24

    Here is one method of accomplishing this:

    lst = [0, 'one', 2.3, 'four', -5]
    a=[x for x in lst if type(x) == type(1) or type(x) == type(1.1)] 
    b=[y for y in lst if type(y) == type('string')]
    a.sort()
    b.sort()
    c = a+b
    print(c)
    

提交回复
热议问题