错误:TypeError:只有长度为1的数组才能转换为Python标量
运行代码时的mi firts错误为:错误:TypeError:只有长度为1的数组才能转换为Python标量,python,python-3.x,numpy,Python,Python 3.x,Numpy,运行代码时的mi firts错误为: TypeError: 'numpy.float64' object cannot be interpreted as an integer 我尝试使用numpy 1.11.0安装,但不起作用。如果y更改为int(),则为新错误: 代码如下: # Load airfoil from file def airfoil_from_file(fname, n=256): dat = np.loadtxt(fname, skiprows=1) eps
TypeError: 'numpy.float64' object cannot be interpreted as an integer
我尝试使用numpy 1.11.0安装,但不起作用。如果y更改为int(),则为新错误:
代码如下:
# Load airfoil from file
def airfoil_from_file(fname, n=256):
dat = np.loadtxt(fname, skiprows=1)
eps=1e-8
num1 = round(dat[0,0])
num2 = round(dat[0,1])
if num1>1:
xy = zeros((num1+num2-1,2))
xy[:num1,:] = dat[1:1+num1,:][::-1,:]
xy[num1:,:] = dat[2+num1:,:]
else:
xy=dat.copy()
第一个错误:
Traceback (most recent call last):
File "compute.py", line 18, in <module>
rx, ry = airfoil_from_file('clarky.dat', n=128)
File "/home/david/Descargas/L04PanelMethod/geometry.py", line 29, in airfoil_from_file
xy = zeros((num1+num2-1,2))
TypeError: 'numpy.float64' object cannot be interpreted as an integer
新错误:
Traceback (most recent call last):
File "compute.py", line 18, in <module>
rx, ry = airfoil_from_file('clarky.dat', n=128)
File "/home/david/Descargas/L04PanelMethod/geometry.py", line 22, in airfoil_from_file
dat = int(np.loadtxt(fname, skiprows=1))
TypeError: only length-1 arrays can be converted to Python scalars
回溯(最近一次呼叫最后一次):
文件“compute.py”,第18行,在
rx,ry=机翼,来自机翼文件('clarky.dat',n=128)
文件“/home/david/Descargas/L04PanelMethod/geometry.py”,第22行,在机翼上
dat=int(np.loadtxt(fname,skiprows=1))
TypeError:只有长度为1的数组才能转换为Python标量
请帮助将原始代码中的xy=0((num1+num2-1,2))更改为xy=0((int(num1+num2)-1,2))
舍入仅舍入,但不转换为int
num1 = round(dat[0,0])
num2 = round(dat[0,1])
在您的情况下,舍入到最接近的浮点,例如舍入(12.4)=12.0。谢谢我更改了它,但现在我有一个新错误:xy[:num1,:]=dat[1:1+num1,:][:::-1,:]类型错误:切片索引必须是整数或无或有索引method@davidnu尼兹·费尔南德斯你在不同的地方犯了完全相同的错误。为什么不尝试同样的解决方案呢?
num1 = round(dat[0,0])
num2 = round(dat[0,1])