错误:索引越界 - Python

时间:2014-12-15 09:46:49

标签: 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.
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

3 个答案:

答案 0 :(得分:0)

正如您在评论中提到的那样,您在这里使用numpy.zeros,代码中theta的值将为

theta = array([0,0,0]) # If size of n is 3

如果您希望将元素添加到theta中 theta = array([0,1,2]),你必须这样做:

theta [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

答案 1 :(得分:0)

问题是numpy的zeros函数会生成类似[[0. 0. 0. 0. ...]]的结构 因此,要访问第一个元素,您需要执行theta[0][0]

你的循环必须像:

for i in range(n):
    theta[0][i] = #your code
对于zeros()生成的所有数组,

同样的事情。

zeros函数返回一个多维数组。

答案 2 :(得分:0)

执行{= 1}时,n = 10将返回

theta = zeros((1,n))

哪个是array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) 。 outter列表只包含索引0处的一个元素,您无法在索引1 ... n

访问它

要解决此问题,请尝试以下操作:

one list contains one list inside
相关问题