[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

First post from a Python newbiw

Steve Turner

3/2/2008 2:15:00 PM

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??

--
Steve


46 Answers

Marc 'BlackJack' Rintsch

3/2/2008 2:25:00 PM

0

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)]

Ciao,
Marc 'BlackJack' Rintsch

Steve Turner

3/2/2008 2:33:00 PM

0

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.

--
Steve

Christoph Zwerschke

3/2/2008 8:59:00 PM

0

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 ?

-- Christoph

Steve Turner

3/2/2008 9:37:00 PM

0

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]

--
Steve

Marc 'BlackJack' Rintsch

3/2/2008 9:49:00 PM

0

On Sun, 02 Mar 2008 21:58:31 +0100, 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 ?

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

Terry Reedy

3/2/2008 10:02:00 PM

0


"Christoph Zwerschke" <cito@online.de> wrote in message
news:fqf4do$gq7$1@online.de...
| 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.



Jeff Schwab

3/2/2008 10:11:00 PM

0

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)]

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.

Aaron Brady

3/2/2008 10:48:00 PM

0

> >>> 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!

Jeff Schwab

3/2/2008 11:24:00 PM

0

castironpi@gmail.com 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. :)

Arnaud Delobelle

3/3/2008 10:33:00 AM

0



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-multidimens...

--
Arnaud