如何找到数组中元素的绝对值?

时间:2018-07-12 04:34:54

标签: python-3.x

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

例如假设我的数组是

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

我想要一个数组

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

2 个答案:

答案 0 :(得分:1)

不使用列表理解功能的numpy

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

print(arr_out)

输出:

  

[2,5,0,1,2]

答案 1 :(得分:0)

import numpy as np

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

print("Absolute value: ", np.absolute(arr))