[Tutor] The Python way and two dimensional lists
David
bouncingcats at gmail.com
Tue Dec 7 03:39:06 EST 2021
On Tue, 7 Dec 2021 at 18:21, Phil <phillor9 at gmail.com> wrote:
> box_start = [(0, 0), (0, 3), (0, 6),
> (3, 0), (3, 3), (3, 6),
> (6, 0), (6, 3), (6, 6)
> ]
>
> for x, y in box_start:
> ...
>
> for col in range(x, x + 3):
> ...
> for row in range(y, y + 3):
> print(f'self.solution[row][col]
> {self.solution[row][col]}')
>
> This works but I know that it's not the correct method and I still find
> accessing columns within a 2D array confusing. I only got this working
> by trial and error by swapping the x and y coordinates. I think all I've
> done is turn the grid onto it's side.
Hi, I'm not going to dig into your code at all.
But I am offering to help, just by making sure that you
understand the basic idea of 2D indexing of lists.
Also I suggest to stop using the word 'array' at this stage.
Python has both 'list' and 'array' and they are slightly
different. So just stick to 'list' for the time being, and
stop calling your lists 'arrays' because that's unclear.
> col = [row[c] for row in self.solution[:3]]
Also I suggest to stop using comprehensions at this
stage. Just use simple 'for' loops like I have, until they
make sense. After that, you can start to use
comprehensions and slicing.
I don't know what it is that you aren't understanding.
But you seem to be unclear so I have written a quick
demo of 2D list indexing below for you.
I suggest you have a look at *and* *run* the code below until you
have no trouble understanding it.
I think that being confident about this concept will make
your actual problem much clearer to you.
I have pasted the code below, but it might get mangled
or line-wrapped by the email system, so I have attached
a file copy of it also.
# have a look at this code, maybe it helps you understand
# i is row index
# j is column index
# lets use a list for each row, each row-list has 3 columns
row_0 = [0, 1, 2]
row_1 = [3, 4, 5]
row_2 = [6, 7, 8]
# how to print each column of row_0
for j in range(0, 3):
print(f"row_0[{j}] = row_0, column {j} = {row_0[j]}")
# lets make box contain 3 rows, let box be a list of 3 rows
box = [row_0, row_1, row_2]
# note: box is a list, of three rows
# note: each row is a list, of three elements
# how to print each row of box
for i in range(0, 3):
print(f"box[{i}] = row_{i} = {box[i]}")
# now combine the above two print ranges, to print the whole box, by rows
for i in range(0, 3):
# get a row, which is one of the three lists in box
row = box[i]
# now print each column of the row
for j in range(0, 3):
# print each j of the box[i] row
print(f"row[{j}] = {row[j]}")
# but we dont really need that variable 'row'
# because row = box[i]
# so instead of naming 'row', we can refer to 'box[i]'
# they are the same thing
# so for example row[2] can be written box[i][2]
# because box[i] is the row list, and then we ask for the [2] element of it
# and by the same thinking, any element can be accessed as box[i][j]
# where box[i] is the row, which is then indexed using [j]
# the below print statement is exactly the same as the previous one
for i in range(0, 3):
for j in range(0, 3):
print(f"box[{i}][{j}] = {box[i][j]}")
-------------- next part --------------
# have a look at this code, maybe it helps you understand
# i is row index
# j is column index
# lets use a list for each row, each row-list has 3 columns
row_0 = [0, 1, 2]
row_1 = [3, 4, 5]
row_2 = [6, 7, 8]
# how to print each column of row_0
for j in range(0, 3):
print(f"row_0[{j}] = row_0, column {j} = {row_0[j]}")
# lets make box contain 3 rows, let box be a list of 3 rows
box = [row_0, row_1, row_2]
# note: box is a list, of three rows
# note: each row is a list, of three elements
# how to print each row of box
for i in range(0, 3):
print(f"box[{i}] = row_{i} = {box[i]}")
# now combine the above two print ranges, to print the whole box, by rows
for i in range(0, 3):
# get a row, which is one of the three lists in box
row = box[i]
# now print each column of the row
for j in range(0, 3):
# print each j of the box[i] row
print(f"row[{j}] = {row[j]}")
# but we dont really need that variable 'row'
# because row = box[i]
# so instead of naming 'row', we can refer to 'box[i]'
# they are the same thing
# so for example row[2] can be written box[i][2]
# because box[i] is the row list, and then we ask for the [2] element of it
# and by the same thinking, any element can be accessed as box[i][j]
# where box[i] is the row, which is then indexed using [j]
# the below print statement is exactly the same as the previous one
for i in range(0, 3):
for j in range(0, 3):
print(f"box[{i}][{j}] = {box[i][j]}")
More information about the Tutor
mailing list