TypeError:' float'对象在3d数组中不可订阅

时间:2016-10-16 00:44:05

标签: python arrays python-3.x object typeerror

我试图编写一个获得三维数组并检查其中有多少个单元格是空的函数。 但我会得到以下错误

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

我的功能如下

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

来自我正在使用的大型模块 这是与之配合使用的功能

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

任何评论都将不胜感激

更新

我拼命运行模块内部的功能,我发现在检查功能中没有问题第一次迭代和i和j的第二次迭代。但之后将面临错误。

这是我的输出:the output of the code that I'm trying to run

你可以看到它在i in check功能的第一次迭代中没有任何问题。
这是我的修复功能。它改变了一些其他细胞相对于刚改变的细胞的箭头。

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

在这里你可以找到整个包裹: my code

1 个答案:

答案 0 :(得分:0)

这意味着m实际上不是3d数组,代码试图在[i]上执行[i][j][i][j][0]float

“containers”是“可编写脚本的”,如here所述