Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

manipulating 3D array in python

I’m getting the following error when I run the code given below. Still learning Python, so where am I going wrong in my understanding ? What is the fix?

Traceback (most recent call last):
File "main.py", line 26, in
cube[1:3, 1:3]= [‘‘, ‘‘, ‘*’]
TypeError: list indices must be integers or slices, not tuple

cube = [[[':(', 'x', 'x'],
         [':)', 'x', 'x'],
         [':(', 'x', 'x'],
         [':(', 'x', 'x']],

        [[':)', 'x', 'x'],  
         [':(', 'x', 'x'], # --> want this element to be ['*', '*', '*']
         [':)', 'x', 'x'], # --> want this element to be ['*', '*', '*']
         [':(', 'x', 'x']],

        [[':(', 'x', 'x'],
         [':)', 'x', 'x'], # --> want this element to be ['*', '*', '*']
         [':)', 'x', 'x'], # --> want this element to be ['*', '*', '*']
         [':(', 'x', 'x']],
         
        [[':(', 'x', 'x'],
         [':)', 'x', 'x'],
         [':)', 'x', 'x'],
         [':(', 'x', 'x']],
         
        [[':(', 'x', 'x'],
         [':)', 'x', 'x'],
         [':)', 'x', 'x'],
         [':(', 'x', 'x']]]
         
cube[1:3, 1:3] = ['*', '*', '*']

print(cube)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are using nested lists, which are not truly 3D arrays. I would suggest you use numpy to get arrays that actually support that kind of slice based access:

import numpy as np
cube = np.array([[[':(', 'x', 'x'],
         [':)', 'x', 'x'],
         [':(', 'x', 'x'],
         [':(', 'x', 'x']],

        [[':)', 'x', 'x'],  
         [':(', 'x', 'x'], 
         [':)', 'x', 'x'], 
         [':(', 'x', 'x']],

        [[':(', 'x', 'x'],
         [':)', 'x', 'x'], 
         [':)', 'x', 'x'], 
         [':(', 'x', 'x']],
         
        [[':(', 'x', 'x'],
         [':)', 'x', 'x'],
         [':)', 'x', 'x'],
         [':(', 'x', 'x']],
         
        [[':(', 'x', 'x'],
         [':)', 'x', 'x'],
         [':)', 'x', 'x'],
         [':(', 'x', 'x']]])
         
cube[1:3, 1:3] = ['*', '*', '*']

print(cube)

Output:

[[[':(' 'x' 'x']
  [':)' 'x' 'x']
  [':(' 'x' 'x']
  [':(' 'x' 'x']]

 [[':)' 'x' 'x']
  ['*' '*' '*']
  ['*' '*' '*']
  [':(' 'x' 'x']]

 [[':(' 'x' 'x']
  ['*' '*' '*']
  ['*' '*' '*']
  [':(' 'x' 'x']]

 [[':(' 'x' 'x']
  [':)' 'x' 'x']
  [':)' 'x' 'x']
  [':(' 'x' 'x']]

 [[':(' 'x' 'x']
  [':)' 'x' 'x']
  [':)' 'x' 'x']
  [':(' 'x' 'x']]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading