Can I ask how to achieve this in Python:
Input:
I = [10,-22,0]
Output:
O = [1,-1,-1]
I was thinking
O=I/abs(I)
But how to deal with zero?
The following should do what you want:
>>> I = [10,-22,0]
>>> O = [1 if v > 0 else -1 for v in I]
>>> O
[1, -1, -1]
>>>
If you want to use map
with a lambda
, you can do:
>>> O = map(lambda v: 1 if v > 0 else -1, I)
>>> O
[1, -1, -1]
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With