Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how can I change the sign of one number in a list based on another number's sign?

I have two values in a list, let's say it's

A = [1, -3]

If the sign of the second value is positive, I can leave the number alone. If the sign of the second value is negative, I need to change the first value to be a -1. But if the first value is a 0, nothin needs to be done at all, since -0 is the same as a positive zero.

So in the case of A, I would need to change 1 to -1. So the final result should be

A = [-1, -3]

I'm a new programmer, so basically all I think is that it involves appending a value to the end of A, and then removing the first value. I think we have to do that because as far as I know, there's no way of just changing the sign of a number.

So, I think it might be something like this:

A = [1, -3]
for i in A:
    if #something about the second value being a positive value
        pass
    else:
        A.append("-1")
        del A[0]
print A

Any help would be appreciated, thanks!

like image 633
Nick Avatar asked Dec 19 '22 20:12

Nick


1 Answers

This works:

if A[1] < 0:
    A[0] *= -1
like image 193
cforbish Avatar answered May 15 '23 19:05

cforbish