[Tutor] Two dimensional lists

Mark Lawrence breamoreboy at gmail.com
Mon Nov 2 01:07:26 EST 2020


On 02/11/2020 01:24, Phil wrote:
> I have some C++ code that I wrote some time ago and, just as an 
> exercise, I'm converting it to Python. I've come to a standstill because 
> of arrays. If I use a numpy array as follows then I can use that array 
> in exactly the same way as a C++ array even though I don't understand 
> the syntax of it. I used the trial_and_error method to create it.
> 
> self.board = numpy.zeros((board_size_x, board_size_y), dtype=numpy.bool)
> self.next_board = numpy.zeros((board_size_x, board_size_y), numpy.bool)
> 
> Python lists, if I understand this correctly, can be used in the same 
> way as arrays. However, I've made little progress. Is there a list 
> equivalent to this?
> 
> b = [5] , [5] # a square array of size 5
> 
> b[3][2] = 9
> 
> In Python, b = [[5,5],[5,5]] looked promising but I'm still greeted with 
> a list out of range error. I've read through several Python tutorials 
> and I've tried append and even though there isn't an error the result 
> doesn't make sense.
> 
> No doubt the solution is straight forward but this poor old duffer 
> cannot see it.
> 

I'm too tired to type much so start here 
https://stackoverflow.com/questions/12791501/python-initializing-a-list-of-lists 
as it's an obvious pitfall :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list