Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
4D array initalization
#1
Hello,

currently I have an issue with initializing a four dimensional array with different lengths in every dimension. I use python 2.7.12 on Windows 7. The following example gives me an error on the array dimension:

a=3
b=4
c=5
d=1

test=[[[[1 for w in range(a)] for x in range(b)] for y in range(c)] for z in range(d)]

for w in range(a):
   for x in range(b):
      for y in range(c):
         for z in range(d):
            print(test[w][x][y][z])
Error:
IndexError: list index out of range
I do not understand this error message since i call the the array "test" within the exact same range I initalizied it. I have already treid fixing this by adding +1 to the range of every dimension of the array test but the error still occurs. Any kind of comment or help on this issue is much appreciated!

Thank you all in advance
Reply
#2
Please post the full error traceback which includes line numbers,

Thanks
Reply
#3
Sure! The full error message is

Error:
Traceback (most recent call last):  File "test.py", line 12, in <module>    print(test[w][x][y][z]) IndexError: list index out of range
Reply
#4
You are using the index in the wrong order.... "w" is you 'inner' index, so you should be using:
print(test[z][y][x][w])
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020