1

I am trying to learn Python 2D arrays and I am trying to add to an empty one but am getting the error list indices must be integers or slices, not tuple

I wrote what I wanted to do in Java and am trying to translate it over to Python

My Java code:

Object[][] memoryArray = new Object[256][17];

        int memoryRow, memoryCol;
        int MBR = 12;
        int addr = MBR;
        memoryRow = addr / 16;
        memoryCol = addr % 16 + 1;
        memoryArray[memoryRow][memoryCol] = " "+Integer.toString(MBR);
        for (Object element: memoryArray) {
            System.out.println(element);
     }

My Python Code:

memArray = [[None], [None]]
MBR = 55
address = MBR 
memR = (address / 16)
memC = (address % 16 + 1)
memArray[[memR], [memC]] = " " + str(MBR)

If anyone could lead me to any pointers on how I should correctly implement this logic in Python? I am not sure what the error is trying to indicate.

I also was wondering if I would be better off using Numpy Arrays?

HotWheels
  • 444
  • 3
  • 23
  • Does this answer your question? [How to define a two-dimensional array?](https://stackoverflow.com/questions/6667201/how-to-define-a-two-dimensional-array) – Amin Rashidbeigi Nov 05 '20 at 21:05
  • Yes! I was confused by the range keyword. @QuangHoang also gave a excellent answer with one-line – HotWheels Nov 05 '20 at 21:19

2 Answers2

2

Java newbies here as well, but let's try translate together:

memArray = [[None], [None]]

is not a Python equivalence for

Object[][] memoryArray = new Object[256][17];

Instead:

memArray = [[None for _ in range(17)] for _ in range(256)]

And

# Java
memoryArray[memoryRow][memoryCol] = " "+Integer.toString(MBR);

translate directly to:

memArray[memR][memC] = " " + str(MBR)

And lastly, while Numpy array might help with advanced indexing over Python's list, if you are working with strings/objects, you wouldn't see much improvement in computing efficiency.

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Your suggestion for ```memArray = [[None for _ in 17] for _ in 256]``` gives me an error ```TypeError: 'int' object is not iterable``` – HotWheels Nov 05 '20 at 21:15
  • 1
    @HotWheels (Blushed icon) Sorry, sometimes I made that silly mistake. See updated answer. – Quang Hoang Nov 05 '20 at 21:16
  • this is a great answer, I did not realize the importance of the ```range``` keyword in Python. – HotWheels Nov 05 '20 at 21:20
0

I think what you want is memArray[memR][memC] = " " + str(MBR) but the way it is it will not work because 1) you have to ensure memR is integer and 2) because your list is empty

Flavio Moraes
  • 1,331
  • 1
  • 5
  • 16
  • Isn't memR ensured as an integer because it takes an integer address/16 and stores it? I assume it does this because there are no type declarations in Python, at least to my knowledge. – HotWheels Nov 05 '20 at 21:16
  • in python 2 if address is integer, memR will be integer, but in python 3 it will be a float unless you use int(address / 16) – Flavio Moraes Nov 05 '20 at 22:32