iterate to make multiple variables?

T

Tairic

Hi, I'm somewhat new to programming and especially to python. Today I
was attempting to make a sudoku-solver, and I wanted to put numbers
into sets call box1, box2, ... box9, so that I could check new values
against the boxes

I ended up declaring the boxes like this
box1 = set([])
box2 = set([])
...
...
box9 = set([])


Is there a way for me instead to generate these variables (box1 to
box9) as empty sets instead of just writing them all out? Some way to
iterate and generate them?

Sorry if this is confusing, thanks in advance.
 
M

Mark Tolonen

Tairic said:
Hi, I'm somewhat new to programming and especially to python. Today I
was attempting to make a sudoku-solver, and I wanted to put numbers
into sets call box1, box2, ... box9, so that I could check new values
against the boxes

I ended up declaring the boxes like this
box1 = set([])
box2 = set([])
..
..
box9 = set([])


Is there a way for me instead to generate these variables (box1 to
box9) as empty sets instead of just writing them all out? Some way to
iterate and generate them?

This will generate a list of nine unique, empty sets, box[0] through box[8]:

box = [set() for i in range(9)]

-Mark
 
P

Pascal Chambon

Mark Tolonen a écrit :
Tairic said:
Hi, I'm somewhat new to programming and especially to python. Today I
was attempting to make a sudoku-solver, and I wanted to put numbers
into sets call box1, box2, ... box9, so that I could check new values
against the boxes

I ended up declaring the boxes like this
box1 = set([])
box2 = set([])
..
..
box9 = set([])


Is there a way for me instead to generate these variables (box1 to
box9) as empty sets instead of just writing them all out? Some way to
iterate and generate them?

This will generate a list of nine unique, empty sets, box[0] through
box[8]:

box = [set() for i in range(9)]

-Mark
Yep, such automatization on local variables is not really meant to be
(even though you can play with eval(), and end up having an unreadable
code), iterables are made for that B-)
 
J

john

Hi, I'm somewhat new to programming and especially to python. Today I
was attempting to make a sudoku-solver, and I wanted to put numbers
into sets call box1, box2, ... box9, so that I could check new values
against the boxes

I ended up declaring the boxes like this
box1 = set([])
box2 = set([])
..
..
box9 = set([])

Is there a way for me instead to generate these variables (box1 to
box9) as empty sets instead of just writing them all out? Some way to
iterate and generate them?

Sorry if this is confusing, thanks in advance.
 
J

john

Hi, I'm somewhat new to programming and especially to python. Today I
was attempting to make a sudoku-solver, and I wanted to put numbers
into sets call box1, box2, ... box9, so that I could check new values
against the boxes

I ended up declaring the boxes like this
box1 = set([])
box2 = set([])
..
..
box9 = set([])

Is there a way for me instead to generate these variables (box1 to
box9) as empty sets instead of just writing them all out? Some way to
iterate and generate them?

Sorry if this is confusing, thanks in advance.

There's a key to answering you. You pointed it out in your question,
when you say '...if
this is confusing, ...'. Most of programming involves writing down
the stuff we need so that
we see it in less-confusing ways. It's called "stepwise refinement"
by the book publishers.
What it means is that you try to think about your problem, mess about
with the tools at hand,
(lists [], sets () , sequences () ,strings " ", and dictionaries
{ : }), and see what might
do for the job. How are they different. You ought to monkey around
with each of them, on your own--don't ask for help, because we can't
help as much as you can help yourself. You'll understand much more,
in the end, besides. Then, when you come back, with some code, even
some code that doesn't work worth a damn, if it's halfway reasonable,
we'll be able to show you where your slight misstep was--but often,
you'll get stuff working, and it's really important that you start to
do it. Mess with the effing interpreter, mess with it often, and look
at different examples, and try them all out. Truly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,218
Messages
2,571,124
Members
47,727
Latest member
smavolo

Latest Threads

Top