Why doesn't Python have a sign function?

前端 未结 12 690
旧巷少年郎
旧巷少年郎 2020-12-02 04:54

I can\'t understand why Python doesn\'t have a sign function. It has an abs builtin (which I consider sign\'s sister), but no si

12条回答
  •  一整个雨季
    2020-12-02 04:57

    Another one liner for sign()

    sign = lambda x: (1, -1)[x<0]
    

    If you want it to return 0 for x = 0:

    sign = lambda x: x and (1, -1)[x<0]
    

提交回复
热议问题