473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

First post from a Python newbiw

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
Mar 2 '08 #1
9 1078
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
Mar 2 '08 #2
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

Mar 2 '08 #3
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
Mar 2 '08 #4
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

Mar 2 '08 #5
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
Mar 2 '08 #6

"Christoph Zwerschke" <ci**@online.dewrote in message
news:fq**********@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.

Mar 2 '08 #7
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.
Mar 2 '08 #8


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/progra...mensional-list

--
Arnaud
Mar 3 '08 #9
Arnaud Delobelle schrieb:
It's a FAQ:
http://www.python.org/doc/faq/progra...mensional-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
Mar 3 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bengt Richter | last post by:
Useless example first: def foo(x): a = suite: y = 1 b = suite: y = 2 a() # equivalent effect to local y = 1 above (a,b)() # effect of suite b if bool(x), otherwise suite a vars()() #...
2
by: Dennis | last post by:
The setup: Windows XP Pro Python 2.3 GTK+ 2.2.1.2 and pyGTK 1.99.17 for Python 2.3 from http://www.pcpm.ucl.ac.be/~gustin/win32_ports/ I've downloaded and installed per the instructions the...
10
by: R.Marquez | last post by:
I hope I don't bore you with this personal experience. But, I hope the details are helpful for other Python and/or Linux newbies, or for those thinking about becoming such. I have been using...
0
by: Rene | last post by:
I'm trying to learn how to use python for cgi scripts... I created a form that submits some info that I put in a cookie. Then the script calls itself and reads the cookie, and displays a...
15
by: Håkan Persson | last post by:
Hi. I am trying to set up a simple HTTP-server but I have problems reading data that is beeing POSTed. class httpServer(BaseHTTPServer.BaseHTTPRequestHandler): def do_POST(self): input =...
27
by: hokiegal99 | last post by:
Hi Guys, This is my first C program. I started programming in Python. Please look over this source and let me know if I'm doing anything wrong. I want to start out right with C, so if you see...
4
by: Redefined Horizons | last post by:
I still new to Python, and I've only dabbled in C, but I've got my first project I need to tackle that involves both languages. I was hoping to get some advice on how to proceed. There is a...
0
by: GeicoGecko | last post by:
Hey guys, There was no python section in the "Web Development" forum so I'm hoping I can pose my question in here. Our system We currently have a python webserver using SimpleHTTPServer,...
4
by: =?GB2312?B?0rvK18qr?= | last post by:
Hi all, I read this interesting post comparing Boost.Python with Pyd: http://pyd.dsource.org/vsboost.html What's your opinion about it? What's your first choice when you have write a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.