简体   繁体   English

TypeError:“ float”对象在3d数组中无法下标

[英]TypeError: 'float' object is not subscriptable in 3d array

guys I'm trying to write a function which get a 3-D array and check how many of its cells are empty. 伙计们,我正在尝试编写一个获取3-D数组并检查其单元格为空的函数。 but i will got the following error 但我会收到以下错误

in checkpoint
if m[i][j][0] == 0:
TypeError: 'float' object is not subscriptable

my function is as following 我的功能如下

def checkpoint(m, i, j):
    c = 0
    if m[i][j][0] == 0:
        c += 1.0
    if m[i][j][1] == 0:
        c += 1.0
    if m[i][j][2] == 0:
        c += 1.0
    if m[i][j][3] == 0:
        c += 1.0
return c

its from a large module that I'm working with here is the function that work with it 它来自我正在使用的大型模块中的功能

def check(m, size):
flag = 2
for i in range(size):
    for j in range(size):
        print(i, j, "/n")
        c = checkpoint(m, i, j)
        s = summ(m, i, j)
        if c == 2:
            if s == 2 or -2:
                flag = 1.0
                if m[i][j][0] == 0:
                    if m[i][j][1] == 0:
                        m[i][j][0] = m[i][j][1] = (-s/2)
                        fix(m, i, j, 0, size)
                        fix(m, i, j, 1, size)
                    elif m[i][j][2] == 0:
                        m[i][j][0] = m[i][j][2] = (-s/2)
                        fix(m, i, j, 0, size)
                        fix(m, i, j, 2, size)
                    else:
                        m[i][j][0] = m[i][j][3] = (-s/2)
                        fix(m, i, j, 0, size)
                        fix(m, i, j, 3, size)
                elif m[i][j][1] == 0:
                    if m[i][j][2] == 0:
                        m[i][j][1] = m[i][j][2] = (-s/2)
                        fix(m, i, j, 1, size)
                        fix(m, i, j, 2, size)
                    elif m[i][j][3] == 0:
                        m[i][j][1] = m[i][j][3] = (-s/2)
                        fix(m, i, j, 1, size)
                        fix(m, i, j, 3, size)
                else:
                    m[i][j][2] = m[i][j][3] = (-s/2)
        if c == 3:
            flag = 1.0
            if m[i][j][0] == 0:
                m[i][j][0] = -s
            elif m[i][j][1] == 0:
                m[i][j][1] = -s
            elif m[i][j][2] == 0:
                m[i][j][2] = -s
            else:
                m[i][j][3] = -s
return m, flag

any comment would be appreciated 任何评论将不胜感激

update: 更新:

i desperately run the function inside the module and i saw that there isn't any problem whit first iteration and second iteration of the i and j in check function. 我拼命在模块内部运行该函数,发现在check函数中i和j的第一次迭代和第二次迭代没有任何问题。 but after that will faced with the error. 但之后将面临错误。

here is my output: the output of the code that I'm trying to run 这是我的输出: 我尝试运行的代码的输出

as you can see it didn't have any problem in first iteration of the i in check function. 如您所见,它在i in check函数的第一次迭代中没有任何问题。
here is my fix function. 这是我的修复功能。 it changes some other cells with respect to the arrow that cell that just changed. 相对于刚刚更改的箭头,它更改了其他一些单元格。

def fix(m, i, j, k, size):
ip = i - 1
jp = j - 1
iz = i + 1
jz = j + 1
if ip < 0:
    ip = size - 1
if jp < 0:
    jp = size - 1
if iz > size - 1:
    iz = 0
if jz > size - 1:
    jz = 0
kp = (k+2) % 4
if k == 0:
    m[i][jz][kp] = -1 * m[i][j][k]
if k == 1:
    m[iz][j][kp] = -1 * m[i][j][k]
if k == 2:
    m[i][jp][kp] = -1 * m[i][j][k]
if k == 3:
    m[ip][j][kp] = -1 * m[i][j][k]
return m

here you can find whole package: my code 在这里您可以找到整个程序包: 我的代码

That means m is not actually 3d array, the code is trying to do [i] or [i][j] or [i][j][0] on a float . 这意味着m实际上不是3d数组,代码试图在float上执行[i][i][j][i][j][0]

"containers" are "scriptable" as described here 所描述的“容器”的“脚本化” 在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM