Python IndexError:双'for'语句的标量变量索引无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25779368/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IndexError: invalid index to scalar variable for double 'for' statement
提问by Isaac
My purpose is to calculate the list of certain variable repetitively using for statement inside of for statement.
我的目的是在 for 语句中重复使用 for 语句计算某些变量的列表。
My code is as follows.
我的代码如下。
for b in range(0,1487):
    c = 18000*b
    alpha = np.arctan(np.mean(v[c:c+18000])/np.mean(u[c:c+18000]))
    beta = np.arctan(np.mean(w[c:c+18000])/np.mean(u[c:c+18000]))
    R01 = R01.reshape((3,3))
    for c1 in range(c,c+18000):
            windvector = np.array([u[c1],v[c1],w[c1]])
            WV = windvector.reshape((3,1))
            m = np.dot(R01,WV)
            m = m.reshape((1,3))
            m = list(m)
            M = M + m
    for c2 in range(0,18000):
            u = M[c2][0]
            A = A + [u]
    m1 = np.mean(A[0:3000])
    m2 = np.mean(A[3000:3000*2])
    m3 = np.mean(A[3000*2:3000*3])
    m4 = np.mean(A[3000*3:3000*4])
    m5 = np.mean(A[3000*4:3000*5])
    m6 = np.mean(A[3000*5:3000*6])
    M = [m1,m2,m3,m4,m5,m6]
    s1 = np.std(A[0:3000])
    s2 = np.std(A[3000:3000*2])
    s3 = np.std(A[3000*2:3000*3])
    s4 = np.std(A[3000*3:3000*4])
    s5 = np.std(A[3000*4:3000*5])
    s6 = np.std(A[3000*5:3000*6])
    S = [s1,s2,s3,s4,s5,s6]
    RN = fabs((np.mean(S)-np.std(A))/np.std(A))
    RNT = RNT + [RN]
As shown in the code, I would like to get 1487 RN values repetitively, but when I ran this code, this stopped just after 1 rotation among expected 1487, showing the error message of "File "RN_stationarity.py", line 25, in alpha = np.arctan(np.mean(v[c:c+18000])/np.mean(u[c:c+18000])) IndexError: invalid index to scalar variable."
如代码所示,我想重复获得 1487 个 RN 值,但是当我运行此代码时,这在预期的 1487 之间旋转 1 次后停止,显示“文件“RN_stationarity.py”的错误消息,第 25 行,在alpha = np.arctan(np.mean(v[c:c+18000])/np.mean(u[c:c+18000])) IndexError:标量变量的索引无效。”
I am not sure why I got this kind of error. I tried few solution in stackoverflow, but it didn't work.
我不确定为什么会出现这种错误。我在 stackoverflow 中尝试了几个解决方案,但没有奏效。
Would you please give some idea or help?
你能提供一些想法或帮助吗?
It will be really appreciated.
真的很感激。
Thank you,
谢谢,
Isaac
以撒
采纳答案by abarnert
Your problem is that you use the name ufor two completely different values. At the start of this loop, it's a nice big array, but in the middle of the loop you reassign it to a scalar, hence the IndexErrorwhen you try to index it the next time through the loop.
您的问题是您将名称u用于两个完全不同的值。在这个循环开始时,它是一个很好的大数组,但在循环中间你将它重新分配给一个标量,因此IndexError当你下次尝试通过循环对其进行索引时。
From the comments:
来自评论:
ushould have total 1487*18000 numbers which I already checked
u应该有我已经检查过的总共 1487*18000 个数字
So, that's how I know it starts off valid.
所以,这就是我知道它开始有效的方式。
But then in the middle of the loop:
但是在循环的中间:
for c2 in range(0,18000):
        u = M[c2][0]
        A = A + [u]
From other comments, Mis a 2D array. So, after this loop, uis a 0D array (that is, a scalar).
从其他评论来看,M是一个二维数组。因此,在此循环之后,u是一个 0D 数组(即标量)。
The solution is to just not use the name ufor two unrelated things. Rename one of them to something else.
解决方案是不要将名称u用于两个不相关的事物。将其中一个重命名为其他名称。
More generally, the solution is to not use meaningless one-letter variable names, because when you do that, it's very hard to avoid accidentally reusing one, and also hard to tell when you've done so.
更一般地,解决方案是不要使用无意义的单字母变量名,因为当你这样做时,很难避免意外重用一个,而且很难判断你何时这样做了。

