0

I have an array of lists, I want to invert the numbers, all negative numbers should be positive and vice versa.

array([[-0.16759927, -0.04342834,  0.9848982 ],
       [-0.45425025,  0.47425876,  0.75414544],
       [ 0.14613204, -0.19651204,  0.96955064],
       [ 0.55392107,  0.80368964,  0.21738078],
       [-0.13777969, -0.14350102,  0.98001235],
       [-0.00225069,  0.00356328,  0.99999112]])

Is there a python way to do this?

4
  • Thanks for sharing your question with us. Have you tried anything so far that you can share it too? Commented Mar 18, 2022 at 23:50
  • And note that, it is not inverting numbers. Inverting number is "the reciprocal of a number is this fraction flipped upside down". Commented Mar 18, 2022 at 23:54
  • 1
    @AmirhosseinKiani the term is negate Commented Mar 18, 2022 at 23:58
  • "Inverting" means different things in different contexts; absent context, it often but does not always mean the multiplicative inverse. The ~ operator is also called "invert" in Python, and is overloaded by declaring an __invert__ dunder method. Commented Mar 19, 2022 at 0:08

2 Answers 2

4

You can just do -array and get the desired result.

Sign up to request clarification or add additional context in comments.

Comments

1

Inverting a number is not switching if a number is positive or negative. It is flipping a fraction upside down. Every number is just the number over one, so you just have to divide one by each number. This should do that:

import numpy as np
array = np.array([[-0.16759927, -0.04342834,  0.9848982 ],
       [-0.45425025,  0.47425876,  0.75414544],
       [ 0.14613204, -0.19651204,  0.96955064],
       [ 0.55392107,  0.80368964,  0.21738078],
       [-0.13777969, -0.14350102,  0.98001235],
       [-0.00225069,  0.00356328,  0.99999112]])
new_array = np.array([1/i for i in array])

Value for new_array:

array([[  -5.9666131 ,  -23.0264385 ,    1.01533336],
       [  -2.20142972,    2.10855357,    1.32600417],
       [   6.84312626,   -5.08874673,    1.03140564],
       [   1.80531136,    1.2442614 ,    4.60022271],
       [  -7.25796378,   -6.96859158,    1.0203953 ],
       [-444.30818993,  280.64030893,    1.00000888]])

But if you just want what you described this should work:

import numpy as np
array = np.array([[-0.16759927, -0.04342834,  0.9848982 ],
       [-0.45425025,  0.47425876,  0.75414544],
       [ 0.14613204, -0.19651204,  0.96955064],
       [ 0.55392107,  0.80368964,  0.21738078],
       [-0.13777969, -0.14350102,  0.98001235],
       [-0.00225069,  0.00356328,  0.99999112]])
new_array = -array

Value for new_array:

array([[ 0.16759927,  0.04342834, -0.9848982 ],
       [ 0.45425025, -0.47425876, -0.75414544],
       [-0.14613204,  0.19651204, -0.96955064],
       [-0.55392107, -0.80368964, -0.21738078],
       [ 0.13777969,  0.14350102, -0.98001235],
       [ 0.00225069, -0.00356328, -0.99999112]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.