array constructor

Tom Holroyd tomh at po.crl.go.jp
Sun Feb 27 20:55:19 EST 2000


Monday hits -- more --

I want a 4x4 array of zeros.  I can do

from Numeric import *
m = zeros((4,4), Float)

but I'd like to do it with ordinary lists.

m = [[0]*4]*4 doesn't work:

>>> m = [[0]*4]*4
>>> m[2][2] = 5
>>> m
[[0, 0, 5, 0], [0, 0, 5, 0], [0, 0, 5, 0], [0, 0, 5, 0]]

because * doesn't make copies.  Do I have to do

>>> m = [[0]*4]
>>> for x in range(3):
...     m.append([0]*4)
... 
>>> m[2][2] = 5
>>> m
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 5, 0], [0, 0, 0, 0]]

While I'm here, string.atof() and float() both barf on "123.45," while the
C atof() doesn't.  A quick check of the code (stropmodule.c) shows that
there is an explicit check for junk at the end of the string.  Is there
some reason for this besides just being annoying?  :-)  I vote to remove
it.

Dr. Tom Holroyd
"I am, as I said, inspired by the biological phenomena in which
chemical forces are used in repetitious fashion to produce all
kinds of weird effects (one of which is the author)."
	-- Richard Feynman, _There's Plenty of Room at the Bottom_




More information about the Python-list mailing list