Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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—将数组转换为列表会导致值更改 >>将numpy作为np导入 >>>a=np.arange(0,2,0.2) >>>a 数组([0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8]) >>>a=a.tolist() >>>a [0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0, 1.2000000000000002, 1.4000000000000001, 1.6, 1.8] >>>a.指数(0.6) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ValueError:0.6不在列表中_Python_Arrays_List_Numpy - Fatal编程技术网

Python—将数组转换为列表会导致值更改 >>将numpy作为np导入 >>>a=np.arange(0,2,0.2) >>>a 数组([0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8]) >>>a=a.tolist() >>>a [0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0, 1.2000000000000002, 1.4000000000000001, 1.6, 1.8] >>>a.指数(0.6) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ValueError:0.6不在列表中

Python—将数组转换为列表会导致值更改 >>将numpy作为np导入 >>>a=np.arange(0,2,0.2) >>>a 数组([0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8]) >>>a=a.tolist() >>>a [0.0, 0.2, 0.4, 0.6000000000000001, 0.8, 1.0, 1.2000000000000002, 1.4000000000000001, 1.6, 1.8] >>>a.指数(0.6) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ValueError:0.6不在列表中,python,arrays,list,numpy,Python,Arrays,List,Numpy,列表中的某些值似乎已更改,我无法使用index()找到它们。我怎样才能解决这个问题?0.6没有改变;它从未出现过: 为了便于显示,数组中的数字是四舍五入的,但数组实际上包含了您随后在列表中看到的值0.6000000000000010.6不能精确地表示为浮点数,因此依赖浮点数进行精确相等的比较是不明智的 找到索引的一种方法是使用容差方法: >>> import numpy as np >>> a = np.arange(0, 2, 0.2) >>&g

列表中的某些值似乎已更改,我无法使用
index()
找到它们。我怎样才能解决这个问题?

0.6
没有改变;它从未出现过:

为了便于显示,数组中的数字是四舍五入的,但数组实际上包含了您随后在列表中看到的值<代码>0.600000000000001
0.6
不能精确地表示为浮点数,因此依赖浮点数进行精确相等的比较是不明智的

找到索引的一种方法是使用容差方法:

>>> import numpy as np
>>> a = np.arange(0, 2, 0.2)
>>> a
array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8])
>>> 0.0 in a
True # yep!
>>> 0.6 in a
False # what? 
>>> 0.6000000000000001 in a
True # oh...
可能的复制品看看如何比较浮动。@ChristianBerendt事实上,我通常采用“绝对ε”方法。
>>> import numpy as np
>>> a = np.arange(0, 2, 0.2)
>>> a
array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ,  1.2,  1.4,  1.6,  1.8])
>>> 0.0 in a
True # yep!
>>> 0.6 in a
False # what? 
>>> 0.6000000000000001 in a
True # oh...
def float_index(seq, f):
    for i, x in enumerate(seq):
         if abs(x - f) < 0.0001:
             return i
>>> float_index(a, 0.6)
3