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]
>>>
You can just do this:
I = [10,-22,0]
output = []
for num in I:
if num <=0:
output.append(-1)
else:
output.append(1)
print output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.