# [Tutor] The Python way and two dimensional lists

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Dec 7 12:46:11 EST 2021

```On Tue, 7 Dec 2021 20:13:16 +1100, Phil <phillor9 at gmail.com> declaimed the
following:

I'd also emphasize using Python names for the Python structures...

A list is a dynamically allocated (automatically resizes if needed)
heterogeneous (mixed content) structure.

An array is a specialized structure optimized for math operations --
mostly superceded by packages like NumPy/SciPy and/or Pandas. The standard
library array module only supports homogeneous arrays -- and essentially
limited to integer, float, and likely complex numbers (Nope: just checked
and complex are not listed: https://docs.python.org/3/library/array.html --
just byte, 16-bit int, 32-bit int, 64-bit int, 32-bit float, 64-bit
double). NumPy array apparently allows for heterogeneous content.

>While I was typing my previous e-mail I thought of a solution.
>
>col = [row[c] for row in self.solution] gives me the entire column. So
>what I need is to extract the first three rows for the top three boxes.
>Then for the next row of boxes I need the middle section of the column
>(rows 3, 4 and 5) and for the last row of boxes I need the last three
>rows of the column. At the moment I don't know how I might do this but
>that's a project for tomorrow.

As mentioned elsewhere, a linear list with a set of functions to
translate (row, col) into the linear index is probably going to be the
simplest route. That is what one would have used in assembly or other early
languages where one just has a chunk of memory (granted, Python lists are
not "just a chunk of memory", but the addressing scheme still works). For

cell_address = row * 9 + col

(or col * 9 + row depending on which you prefer -- row-major or
column-major)

You could make that a short function and just use

where board is the 81 element list.

Or, if the board is a class instance... (watch out for line wrap).
There is no error checking in the following. Coordinates should be checked
for range 0..8, values maybe should be checked to be set type.

-=-=-
"""
Rudimentary class for accessing Sudoko board
Dennis L Bieber         Dec 7 2021
"""

class Board(object):
def __init__(self):
self._cells = [{_} for _ in range(9 * 9)]
return row * 9 + col
def get_cell(self, row, col):
def set_cell(self, row, col, value):
def get_row(self, row):
return [self._cells[self._cell_address(row, col)] for col in
range(9)]
def set_row(self, row, values):
= values
def get_col(self, col):
return [self._cells[self._cell_address(row, col)] for row in
range(9)]
def set_col(self, col, values):
for row, value in enumerate(values):
-=-=-

C:\Users\Wulfraed\Documents\_Hg-Repositories\Python Progs>python3
Python ActivePython 3.8.2 (ActiveState Software Inc.) based on
on win32
>>> import SudokuBoard as sb
>>> b = sb.Board()
>>> b._cells
[{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13},
{14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25},
{26}, {27}, {28}, {29}, {30}, {31}, {32}, {33}, {34}, {35}, {36}, {37},
{38}, {39}, {40}, {41}, {42}, {43}, {44}, {45}, {46}, {47}, {48}, {49},
{50}, {51}, {52}, {53}, {54}, {55}, {56}, {57}, {58}, {59}, {60}, {61},
{62}, {63}, {64}, {65}, {66}, {67}, {68}, {69}, {70}, {71}, {72}, {73},
{74}, {75}, {76}, {77}, {78}, {79}, {80}]
>>> b.get_cell(1, 5)
{14}
>>> b.set_cell(1, 5, {2, 3})
>>> b.get_cell(1, 5)
{2, 3}
>>> b.get_row(1)
[{9}, {10}, {11}, {12}, {13}, {2, 3}, {15}, {16}, {17}]
>>> b.get_col(5)
[{5}, {2, 3}, {23}, {32}, {41}, {50}, {59}, {68}, {77}]
>>>

I'd have preferred to make those get/set methods Python properties, but
I don't know how to manage a property that takes in multiple arguments
(need row, col, and value for setter).

--
Wulfraed                 Dennis Lee Bieber         AF6VN
wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

```