[Tutor] Creating a two dimension set
Cameron Simpson
cs at cskk.id.au
Mon Nov 1 04:52:49 EDT 2021
On 01Nov2021 17:18, Phil <phillor9 at gmail.com> wrote:
>Problem solved. Actually, the solution had nothing to do with my
>question. Too much looking and not enough seeing. The following works:
>
>num_rows = 2
>num_cols = 2
>
>grid = [[str] * num_cols for _ in range(num_rows)] # I changed None to
>str but I don't thing it matters.
Bare "str" isn't a good thing. Use None, it is the idiomatic Python
value for "no value here" (there's a value, but it isn't in the expected
range). Bare "str" means you've filled your lists with references to the
str type. Kind of weird.
>for row in range(2):
> for col in range(2):
> grid[row][col] = set({'1', '2', '3', '4', '5', '6', '7', '8', '9'})
You don't need to say "set({....})". The "{.....}" stuff is already a
set. Except for "{}" which is an empty dict - for an empty set you need
to say "set()".
I wouldn't use strings. If you're doing Sudoku don't you want to do
arithmetic to check the sum? May as well make a set of ints:
{1, 2, 3, 4, 5, 6, 7, 8, 9}
>grid[0][0].remove('3')
Yes. Of course, using ints would be grid[0][0][.remove(3). Adjust to
suit.
If you wanted to simplify the setup you could fill the grid with empty
sets instead and have it represent rejected values instead of potential
values. Of course you'd have to turn some other logic upside down.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list