简体   繁体   中英

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. 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. 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.
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 .

"containers" are "scriptable" as described here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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