Re: First post from a Python newbiw

2008-03-03 Thread Arnaud Delobelle


Steve Turner wrote:
 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

--
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-03 Thread Christoph Zwerschke
Arnaud Delobelle schrieb:
 It's a FAQ:
 http://www.python.org/doc/faq/programming/#how-do-i-create-a-multidimensional-list

Somewhere on my todo list I have read through the whole Python FAQ, 
but so far never got round doing it. Should probably set it to prio A.

-- Christoph
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread Steve Turner
Marc 'BlackJack' Rintsch wrote: 

: On Sun, 02 Mar 2008 14:15:09 +, 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.

-- 
Steve

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread Marc 'BlackJack' Rintsch
On Sun, 02 Mar 2008 14:15:09 +, 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)]

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread Christoph Zwerschke
Marc 'BlackJack' Rintsch schrieb:
 On Sun, 02 Mar 2008 14:15:09 +, 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 ?

-- Christoph
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread Steve Turner
Christoph Zwerschke wrote: 

: Marc 'BlackJack' Rintsch schrieb:
:: On Sun, 02 Mar 2008 14:15:09 +, 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]

-- 
Steve

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread Marc 'BlackJack' Rintsch
On Sun, 02 Mar 2008 21:58:31 +0100, Christoph Zwerschke wrote:

 Marc 'BlackJack' Rintsch schrieb:
 On Sun, 02 Mar 2008 14:15:09 +, 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:

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread Terry Reedy

Christoph Zwerschke [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
| Marc 'BlackJack' Rintsch schrieb:
|  On Sun, 02 Mar 2008 14:15:09 +, 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.



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread Jeff Schwab
Christoph Zwerschke wrote:
 Marc 'BlackJack' Rintsch schrieb:
 On Sun, 02 Mar 2008 14:15:09 +, 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)]

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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread 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,)*3,)*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!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First post from a Python newbiw

2008-03-02 Thread Jeff Schwab
[EMAIL PROTECTED] wrote:
 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,)*3,)*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. :)
-- 
http://mail.python.org/mailman/listinfo/python-list