Simulating multi-dim array problem - OR - reference confusions
Shane Hoversten
srh232 at nyu.edu
Tue Apr 16 17:35:24 EDT 2002
Hi -
I'm doing code that needs the functionality of multi-dimensional arrays.
I'm getting around this right now by using lists of lists, which seems
to work right. For instance:
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b = a[0]
>>> b[0]
1
>>> a[0][0] = 20
>>> b[0]
20
This is all well and good; it seems to imply that when you say:
b = a[0]
That b is actually a reference to the appropriate list in a, which is
just what I need. The problem is that I'm writing a program that needs
to work with a chained structure. Specifically, each element of the
list of lists contains a "pointer" to another element in the list of
lists. These "pointers" can be traversed much like a linked list. In
other words, an "element" looks like:
[x, y, z, <PTR>]
where <PTR> is set the way I described above, with b = a[0], or, in this
case, a[0][3] = a[10], etc.
Here's are my questions:
1) Am I going to get into trouble assuming (using the example above) that
b is basically a reference to the sublist a[0]? I'm not clear how
sharing works in python.
2) Is there a way I can print out the address of a "reference"? In
particular I want some concise symbolic representation - like a memory
address, or a handle id, or SOMETHING - and not a verbatim
reconstruction of the list structure which is how python's "print"
function does it. If I have elements containing references to elements
containing references to elements it's impossible to follow what's going
on with the chain. I'd like to have it print something like:
[[x, y, z, 0x123123], [a, b, c, 0x123958], [q, w, e, 0x358481]]
or something to that effect, instead of interpolating and making a huge mess.
I've tried to find references (no pun intended) to these issues but have
come up empty. I know the info is out there. Can someone either tell
me or tell me where to find it?
Thanks,
Shane
More information about the Python-list
mailing list