Python 3.x 如何找到数组中元素的绝对值?
例如,假设我的数组是Python 3.x 如何找到数组中元素的绝对值?,python-3.x,Python 3.x,例如,假设我的数组是 I have an array containing some negative values. how can i find the absolute value? 我想要一个数组 arr_out=[2,5,0,1,2]不使用numpy,使用列表理解: import numpy as np arr = [-2,-5,0,1,2] print("Absolute value: ", np.absolute(arr)) 输出: [2,5,0,1,2] 可以从旧数组创
I have an array containing some negative values. how can i find the absolute value?
我想要一个数组
arr_out=[2,5,0,1,2]不使用numpy,使用列表理解:
import numpy as np
arr = [-2,-5,0,1,2]
print("Absolute value: ", np.absolute(arr))
输出:
[2,5,0,1,2]
可以从旧数组创建新数组,并将负值转换为正值
import numpy as np
arr = [-2,-5,0,1,2]
print("Absolute value: ", np.absolute(arr))
arr = [-2,-5,0,1,2]
arr_out = [abs(i) for i in arr]
print(arr_out)