jeudi 4 mars 2021

Function in python that outputs either -1 or 0 or 1

Is there like a (modified) round function where the output would be either -1,0,1 depending on to what number is the input the closest? E.g. 0 => 0, -2154 => -1, 10 => 1

Currently I am using normal if else statements:

if (i==0):
    return 0
elif i > 0: 
    return 1
else:
    return -1

But is there any way how I can make this a one-line code? By for instance using some modified round function.

Aucun commentaire:

Enregistrer un commentaire