Re: How to do multi-dimensional arrays?

Steven D. Majewski (sdm7g@virginia.edu)
Thu, 19 Jan 1995 19:59:32 -0500 (EST)

On Thu, 19 Jan 1995, Aaron Watters wrote:

> The fix to avoid shared references is just a bit less elegant,
> but not too bad:
> >>> arr = [None]*4
> >>> for i in range(4): arr[i] = [0]*4
> ...
> >>> arr[1][2] = 42
> >>> arr
> [[0, 0, 0, 0], [0, 0, 42, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
> Yours, not feeling like working right now, -a.

OK. I'll call!
How about:

>>> arr = map( lambda x: [x]*4, [0]*4 )
>>> arr
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> arr[1][2] = 42
>>> arr
[[0, 0, 0, 0], [0, 0, 42, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>>

Unfortunately, this doesn't appear to recursively propagate to
higher dimensions. :-(

>>> XXX = map( lambda x: [x]*4, map( lambda x: [x]*4, [0]*4 ) )
>>> XXX[1][2][3] = 123
>>> XXX
[[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 123], [0,
0, 0, 123], [0, 0, 0, 123], [0, 0, 0, 123]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0,
0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]

---| Steven D. Majewski (804-982-0831) <sdm7g@Virginia.EDU> |---
---| Computer Systems Engineer University of Virginia |---
---| Department of Molecular Physiology and Biological Physics |---
---| Box 449 Health Science Center Charlottesville,VA 22908 |---