错误:索引超出范围-Python

错误:索引超出范围-Python,python,for-loop,numpy,indexing,Python,For Loop,Numpy,Indexing,我对Python还是相当陌生的,我正在尝试运行for循环。但是,我收到一个错误,指示我的索引超出范围。我不确定到底是什么问题,感谢您的帮助 我的代码和错误如下: croot = 1 ctip = 1 span = 1 thetaroot = 0 thetatip = 0 a0root = 0.11 a0tip = 0.11 alpha = 0 alpha0root = -2.5 alpha0tip = -2.5 thetaroot = thetaroot * atan(1.) / 45. the

我对Python还是相当陌生的,我正在尝试运行for循环。但是,我收到一个错误,指示我的索引超出范围。我不确定到底是什么问题,感谢您的帮助

我的代码和错误如下:

croot = 1
ctip = 1
span = 1
thetaroot = 0
thetatip = 0
a0root = 0.11
a0tip = 0.11
alpha = 0
alpha0root = -2.5
alpha0tip = -2.5
thetaroot = thetaroot * atan(1.) / 45.
thetatip = thetatip * atan(1.) / 45.
alpha = alpha * atan(1.) / 45.
alpha0root = alpha0root * atan(1.) / 45.
alpha0tip = alpha0tip * atan(1.) / 45.
n = 10
theta = zeros((1,n))
y = zeros((1,n))
c = zeros((1,n))
cl = zeros((1,n))
alp = zeros((1,n))
a = zeros((1,n))
rhs = zeros((n,1))
b = zeros((n,1))
a = zeros((n,1))
#
# Define properties at n span stations
#
pi = 4. * atan(1.)
for i in range(1,n):
    theta[i] = i * pi / (2. * n)
    y[i] = span * 0.5 * cos(theta[i])
    c[i] = croot + (ctip - croot) * y[i] * 2. / span
    alp[i] = alpha + thetaroot - (alpha0root + (alpha0tip - alpha0root + thetaroot - thetatip) * y[i] * 2. / span)
    a[i] = a0root + (a0tip - a0root) * y[i] * 2. / span

pi = 4. * atan(1)
然后我收到了错误

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-8-8710173d8f43> in <module>()
     29 pi = 4. * atan(1.)
     30 for i in range(1,n):
---> 31     theta[i] = i * pi / (2. * n)
     32     y[i] = span * 0.5 * cos(theta[i])
     33     c[i] = croot + (ctip - croot) * y[i] * 2. / span

IndexError: index 1 is out of bounds for axis 0 with size 1

正如您在评论中提到的,您在这里使用的是numpy.zeros,代码中θ的值将是

theta = array([0,0,0]) # If size of n is 3
如果您希望向θ添加元素,如 θ=数组[0,1,2],你必须这样做:

θ[0][i]=i*pi/2.*n

这也和变量y,c,alp,a有关。这将是代码:

croot = 1
ctip = 1
span = 1
thetaroot = 0
thetatip = 0
a0root = 0.11
a0tip = 0.11
alpha = 0
alpha0root = -2.5
alpha0tip = -2.5
thetaroot = thetaroot * atan(1.) / 45.
thetatip = thetatip * atan(1.) / 45.
alpha = alpha * atan(1.) / 45.
alpha0root = alpha0root * atan(1.) / 45.
alpha0tip = alpha0tip * atan(1.) / 45.
n = 10
theta = zeros((1,n))
y = zeros((1,n))
c = zeros((1,n))
cl = zeros((1,n))
alp = zeros((1,n))
a = zeros((1,n))
rhs = zeros((n,1))
b = zeros((n,1))
a = zeros((n,1)) # There are 2 definitions of a here which is not good
#
# Define properties at n span stations
#
pi = 4. * atan(1.)
for i in range(1,n):
    theta[0][i] = i * pi / (2. * n)
    y[0][i] = span * 0.5 * cos(theta[0][i])
    c[0][i] = croot + (ctip - croot) * y[i] * 2. / span
    alp[0][i] = alpha + thetaroot - (alpha0root + (alpha0tip - alpha0root + thetaroot - thetatip) * y[0][i] * 2. / span)
    a[i][0] = a0root + (a0tip - a0root) * y[0][i] * 2. / span
    #Or use a[0][i] if a is defined as a = zeros((1, n)), the first definition

pi = 4. * atan(1)
如果您希望添加以下元素:

theta = array([0,0,0], [1,1,1], [2,2,2])
然后修改for循环,如下所示:

for i in range(1,n):
    theta += i * pi / (2. * n)
    y += span * 0.5 * cos(theta[i])
    c += croot + (ctip - croot) * y[i] * 2. / span
    alp += alpha + thetaroot - (alpha0root + (alpha0tip - alpha0root + thetaroot - thetatip) * y[i] * 2. / span)
    a += a0root + (a0tip - a0root) * y[i] * 2. / span
问题是numpy的zeros函数生成类似[[0.0.0.0….]的结构 因此,要访问第一个元素,需要执行θ[0][0]

您的循环需要如下所示:

for i in range(n):
    theta[0][i] = #your code
对于所有由零生成的数组,情况也是如此

zeros函数返回多维数组。

执行θ=zeros1时,n=10的n将返回

array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
其中一个列表包含一个列表。outter列表在索引0处仅包含一个元素,您无法在索引1处访问它…n

要解决此问题,请尝试以下方法:

theta[0][i] = i * pi / (2. * n)

我假设您使用的是numpy?是的,numpy是导入的。正如下面指出的,您应该使用range0,n或仅仅使用range,这是一样的,因为python是0索引的,而不是1索引的,比如说八度。但从您正在做的事情来看,您似乎可以以“矢量化”的方式完成这项工作,而根本不使用循环。您的代码修复了大部分行,但在第35行调用[0][I]时,我仍然收到一个错误。和以前一样的错误。那是因为我错过了后面的a,它生成了zerosn,1。我会修正我的答案。谢谢:-