Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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 ValueError:无法将字符串转换为浮点_Python_Numpy Ndarray - Fatal编程技术网

Python ValueError:无法将字符串转换为浮点

Python ValueError:无法将字符串转换为浮点,python,numpy-ndarray,Python,Numpy Ndarray,我有下面的代码,其中输入是h_ply=['0.12,0.15,0.2,0.125'] h_ply = simpledialog.askstring('Laminate Properties','Ply Thickness') try: h_layer_list = [int(x) for x in h_ply.split(',')] h_layer = np.array(h_layer_list) * 0.001 我也试过了 h_ply1 = np.array(h_ply)

我有下面的代码,其中输入是
h_ply=['0.12,0.15,0.2,0.125']

h_ply = simpledialog.askstring('Laminate Properties','Ply Thickness')
try:
    h_layer_list = [int(x) for x in h_ply.split(',')] 
    h_layer = np.array(h_layer_list) * 0.001 
我也试过了

h_ply1 = np.array(h_ply)
h_layer = h_ply1.astype(np.float) 
但也犯了同样的错误

我试图将
h_ply
列表放入一个
np.array
浮点数组中。但出现了一个错误。 我明白这可能是因为我应该有
''
每个数字,而不仅仅是在末尾?但理想情况下,我希望避免这种情况,以便更方便用户。消除此错误消息的最佳方法是什么


谢谢

处理此问题的方法是正确访问字符串。查看您的数据:

h_ply =(       # a tuple, containing ...
         [     # a single element, a list, containing ...
           [   # a single element, a list, containing ...
             '0.12, 0.15, 0.2, 0.125'     # ... a string that you have to split
           ]
         ]
       )
您必须剥下所有这些层才能接触到字符串:

h_str = h_ply[0][0][0]

现在可以拆分字符串并将元素转换为数值。您发布的代码正在尝试拆分元组并对其进行转换。

将错误的完整回溯显示为问题中格式正确的文本。为了确保这一点,我应该将其拆分为
h_ply=['0.12,0.15,0.2,0.125']
,然后它应该可以工作吗?不,您必须将其拆分为字符串。我真的很抱歉,我是Python新手,所以我跟不上。请举个例子好吗?Stack Overflow并不是为了取代现有的文档和教程。我确实给了你一个例子。如果您不了解这些数据结构之间的差异,那么我们需要您返回您的教育材料。个别教程不在堆栈溢出的范围内。