Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 如何在列表中查找值最接近0的元素_Python 3.x_List_Loops - Fatal编程技术网

Python 3.x 如何在列表中查找值最接近0的元素

Python 3.x 如何在列表中查找值最接近0的元素,python-3.x,list,loops,Python 3.x,List,Loops,假设我有一个列表,x=[-7,15,-6,-19,27,71,3,-84,-16,-4,-15,-28,18,62,-93] 如何找到最接近零的值?使用带有自定义键的min()函数: x = [-7, 15, -6, -19, 27, 71, 3, -84, -16, -4, -15, -28, 18, 62, -93] print(min(x, key=abs)) 印刷品: 3

假设我有一个列表,
x=[-7,15,-6,-19,27,71,3,-84,-16,-4,-15,-28,18,62,-93]

如何找到最接近零的值?

使用带有自定义键的
min()
函数:

x = [-7, 15, -6, -19, 27, 71, 3, -84, -16, -4, -15, -28, 18, 62, -93]

print(min(x, key=abs))
印刷品:

3