簡體   English   中英

錯誤:索引超出范圍 - Python

[英]Error: Index out of bounds - Python

我對 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 值將是

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

如果你想像 theta = array([0, 1, 2]) 那樣向 theta 添加元素,你必須這樣做:

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

問題是 numpy 的zeros function 生成類似[[0. 0. 0. 0....]] [[0. 0. 0. 0....]]所以要訪問第一個元素,您需要執行theta[0][0]

你的循環需要像:

 for i in range(n): theta[0][i] = #your code

zeros()生成的所有數組都是一樣的。

zeros function 返回一個多維數組。

當 n = 10 執行theta = zeros((1,n))時,將返回

array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])

這是one list contains one list inside 外部列表在索引 0 處僅包含一個元素,您無法在索引 1 處訪問它....n

要解決此問題,請嘗試以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM