Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何找到数组中元素的绝对值?_Python 3.x - Fatal编程技术网

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)