0
I have an array containing some negative values. how can i find the absolute value?

for eg suppose my array is

arr = [-2,-5,0,1,2]

and i want an array

arr_out =[2,5,0,1,2]

Derrick
  • 3,669
  • 5
  • 35
  • 50
Bidisha Das
  • 177
  • 2
  • 11
  • You can created new Array from older array and convert negative values to positive https://stackoverflow.com/questions/3854310/how-to-convert-a-negative-number-to-positive – Binit Ghetiya Jul 12 '18 at 05:10

2 Answers2

1

Without numpy using list comprehension:

arr = [-2,-5,0,1,2]
arr_out = [abs(i) for i in arr]

print(arr_out)

Output:

[2, 5, 0, 1, 2]

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0
import numpy as np

arr = [-2,-5,0,1,2] 

print("Absolute value: ", np.absolute(arr))
sjdm
  • 591
  • 4
  • 10