0

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

  • Please show a valid sample of the data you are dealing with. How exactly are you calling your method? – idjaw Oct 16 '16 at 00:44
  • i did update the question – Hamid Shahrokhshahi Oct 16 '16 at 00:56
  • The exception traceback shows that the exception is actually happening in the `checkpoint` function, not directly in the part of the code you've shown. In any case, the specific error probably means that your `m` data structure is getting messed up somewhere (it's got a float where you expect an inner list). Since we only see valid assignments in the `check` function you've shown, the error must be somewhere else (e.g. in `fix` which you call all over, or in some part of `checkpoint` maybe). You may need to post more code! Also, please post the traceback as text, rather than as a screenshot. – Blckknght Oct 16 '16 at 02:24
  • `m` may have bad data before you even call this code. Do you still get it if you put a `break` after `c = checkpoint(m, i, j)`? How about a debug function whose only job is to test the array and raise an exception on badness? You could place that in several parts of your code. BTW, this code is really long... how about a toy program that is shorter and demonstrates the problem? – tdelaney Oct 16 '16 at 02:51

1 Answers1

0

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

Community
  • 1
  • 1
bits
  • 1,595
  • 1
  • 17
  • 17
  • So you mean i should change the way i create my array? do you have any suggestion? – Hamid Shahrokhshahi Oct 16 '16 at 01:06
  • i dont want to use the array in numpy because they will increase my computation time. – Hamid Shahrokhshahi Oct 16 '16 at 01:06
  • I dont see how you are initializing m , but yes numpy will be more efficient and faster than python list of lists to create 2d array ( or higher dimensions) . Details on comparison here: http://stackoverflow.com/questions/993984/why-numpy-instead-of-python-lists – bits Oct 16 '16 at 01:14
  • dude i did update the question. could you look through it again? I just find that there isn't any problem in the first iteration of my check function. the problem will happen in the the beginning of second iteration – Hamid Shahrokhshahi Oct 16 '16 at 02:01