First post from a Python newbiw

S

Steve Turner

I finally decided to have a go with Python and am working through the
tutorial.

On my old BBC Computer I could do something like this:

DIM A(2,2)

to create a 3 by 3 array of data. Then I could set any point:

A(0,0) = foo
A(0,1) = bar
etc.

In Python I thought I could do this with:
a=[0,0,0]
b=[a,a,a]
b [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
b[1][1]='foo'
b [[0, 'foo', 0], [0, 'foo', 0], [0, 'foo', 0]]

I can understand why as b[1][1]='foo' is actually changing a[1]

Apart from doing something like
a=[0,0,0]
b=[0,0,0]
c=[0,0,0]
d=[a,b,c]

is there a better way of creating d??
 
M

Marc 'BlackJack' Rintsch

Apart from doing something like
a=[0,0,0]
b=[0,0,0]
c=[0,0,0]
d=[a,b,c]

is there a better way of creating d??

a = [[0] * 3 for dummy in xrange(3)]

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steve Turner

Marc 'BlackJack' Rintsch wrote:

: On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
:
:: Apart from doing something like
:: a=[0,0,0]
:: b=[0,0,0]
:: c=[0,0,0]
:: d=[a,b,c]
::
:: is there a better way of creating d??
:
: a = [[0] * 3 for dummy in xrange(3)]

Thanks, Marc.
 
S

Steve Turner

Christoph Zwerschke wrote:

: Marc 'BlackJack' Rintsch schrieb:
:: On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
::
::: Apart from doing something like
::: a=[0,0,0]
::: b=[0,0,0]
::: c=[0,0,0]
::: d=[a,b,c]
:::
::: is there a better way of creating d??
::
:: a = [[0] * 3 for dummy in xrange(3)]
:
: Why not simply [[0]*3]*3 ?

I've just tried that and it gives the same as my earlier b=[a,a,a]
 
M

Marc 'BlackJack' Rintsch

Marc said:
Apart from doing something like
a=[0,0,0]
b=[0,0,0]
c=[0,0,0]
d=[a,b,c]

is there a better way of creating d??

a = [[0] * 3 for dummy in xrange(3)]

Why not simply [[0]*3]*3 ?

Because:

In [77]: a = [[0] * 3] * 3

In [78]: a
Out[78]: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

In [79]: a[0][0] = 42

In [80]: a
Out[80]: [[42, 0, 0], [42, 0, 0], [42, 0, 0]]

Ciao,
Marc 'BlackJack' Rintsch
 
T

Terry Reedy

| Marc 'BlackJack' Rintsch schrieb:
| > On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
| >
| >> Apart from doing something like
| >> a=[0,0,0]
| >> b=[0,0,0]
| >> c=[0,0,0]
| >> d=[a,b,c]
| >>
| >> is there a better way of creating d??
| >
| > a = [[0] * 3 for dummy in xrange(3)]
|
| Why not simply [[0]*3]*3 ?

Because that is essentially the same as what the OP originally did,
which does not work as he wanted.
 
C

castironpi

is there a better way of creating d??
a = [[0] * 3 for dummy in xrange(3)]

Each element of a refers to a distinct array.
Why not simply [[0]*3]*3 ?

All three elements of the result refer to the same array.

.... whereas you reassign all three elements of [0]* 3.
((0, 0, 0), (0, 0, 0), (0, 0, 0))

You're safe in this one-- changing [0][0] won't change [1][0], 'cuz
you can't!
 
J

Jeff Schwab

is there a better way of creating d??
a = [[0] * 3 for dummy in xrange(3)]
Each element of a refers to a distinct array.
Why not simply [[0]*3]*3 ?
All three elements of the result refer to the same array.

... whereas you reassign all three elements of [0]* 3.
((0, 0, 0), (0, 0, 0), (0, 0, 0))

You're safe in this one-- changing [0][0] won't change [1][0], 'cuz
you can't!

A technically correct solution. :)
 
A

Arnaud Delobelle

Steve said:
I finally decided to have a go with Python and am working through the
tutorial.
Great!

On my old BBC Computer [...]

These were nice machines...
In Python I thought I could do this with:
a=[0,0,0]
b=[a,a,a]
b [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
b[1][1]='foo'
b [[0, 'foo', 0], [0, 'foo', 0], [0, 'foo', 0]]

I can understand why as b[1][1]='foo' is actually changing a[1]

Apart from doing something like
a=[0,0,0]
b=[0,0,0]
c=[0,0,0]
d=[a,b,c]

is there a better way of creating d??

It's a FAQ:
http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list
 

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
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top