nested structure with "internal references"

T

Torsten Mohr

Hi,

sorry for posting in german before, that was a mistake.

I'd like to use a nested structure in memory that consists
of dict()s and list()s, list entries can be dict()s, other list()s,
dict entries can be list()s or other dict()s.

The lists and dicts can also contain int, float, string, ...

But i'd also like to have something like a "reference" to another
entry.
I'd like to refer to another entry and not copy that entry, i need to
know later that this is a reference to another entry, i need to find
also access that entry then.

Is something like this possible in Python?

The references only need to refer to entries in this structure.
The lists may change at runtime (entries removed / added), so
storing the index may not help.


Thanks for any hints,
Torsten.
 
A

akonsu

Hi,

sorry for posting in german before, that was a mistake.

I'd like to use a nested structure in memory that consists
of dict()s and list()s, list entries can be dict()s, other list()s,
dict entries can be list()s or other dict()s.

The lists and dicts can also contain int, float, string, ...

But i'd also like to have something like a "reference" to another
entry.
I'd like to refer to another entry and not copy that entry, i need to
know later that this is a reference to another entry, i need to find
also access that entry then.

Is something like this possible in Python?

The references only need to refer to entries in this structure.
The lists may change at runtime (entries removed / added), so
storing the index may not help.

Thanks for any hints,
Torsten.

hello,

maybe i know less python than you do, but i am talking from the point
of view of my common sense and experience. if you store an object in a
list more than once, i doubt that python creates a duplicate. unless
the object is a value type. thus you have a reference to this object
in both elements. and then if you are asking this question, maybe your
design is not good enough? would you explain the problem?
konstantin
 
C

Carl Banks

Hi,

sorry for posting in german before, that was a mistake.

I'd like to use a nested structure in memory that consists
of dict()s and list()s, list entries can be dict()s, other list()s,
dict entries can be list()s or other dict()s.

The lists and dicts can also contain int, float, string, ...

But i'd also like to have something like a "reference" to another
entry.
I'd like to refer to another entry and not copy that entry, i need to
know later that this is a reference to another entry, i need to find
also access that entry then.

Is something like this possible in Python?

The references only need to refer to entries in this structure.
The lists may change at runtime (entries removed / added), so
storing the index may not help.

Hmm, I think I understand what you're asking but I don't think there's
an easy way.

Probably the most straightforward way is to use "cells" of some sort
to contain the entries in a list or dict; then take references to the
cells. That way you can change the cell's value while still
maintaining the reference. For instance, suppose you have a list like
this:

lst = [ 1, 2, 3, 4 ]

Convert it to this:

lst = [ [1], [2], [3], [4] ]

where you are using a nested singleton list as the cell type. Then,
you can take a reference like this:

ref = lst[2]

If you insert an item into the list, the reference remains valid:

lst.insert(0,[0])

And, if you change the value of the cell, the reference will see the
updated value.

lst[3][0] = 5

The downside is that you have to add [0] everywhere. The reference's
value is ref[0], not just ref. The third item in the list is lst[2]
[0], not just lst[2]. You could define custom list- and dict-like
classes that automatically do this, mitigating the problem somewhat.

I hope you could understand that suggestion, it's tough to describe.


Other than that, you are stuck with massive use of Observer pattern
(you'd have to create list- and dict-like types that know when
something is referencing them, and that notify the references whenever
they change).


Carl Banks
 
A

akonsu

put a (name, value) pair in each list element instead of just value
and reference them by name, you can use uuid to generate names

konstantin
 
K

Kent Turbo

I'd like to refer to another entry and not copy that entry, i need to
know later that this is a reference to another entry, i need to find
also access that entry then.

The references only need to refer to entries in this structure.
The lists may change at runtime (entries removed / added), so
storing the index may not help.

Python already stores everything as a reference, no need to invent
your own.
Consider this example:
data = "This is real data"
container = [ data, [ data ]]
container
['This is real data', ['This is real data']]

'container' contains a reference to the data, and another list, with
another reference. You can easily check this by using a built-in
function id() which returns an object's identity:
id(container[0]) 140191569359088
id(container[1][0]) 140191569359088
id(data)
140191569359088

So all python lists and dicts hold are references, and the real data
is elsewhere. Do you really need to have real reference to your data
in one place, or you are trying to avoid infinite loops in your data
(python handles this as well)?
 
H

Hendrik van Rooyen

I'd like to use a nested structure in memory that consists
of dict()s and list()s, list entries can be dict()s, other list()s,
dict entries can be list()s or other dict()s.

The lists and dicts can also contain int, float, string, ...

This sounds terribly convoluted.
What are you trying to do?
But i'd also like to have something like a "reference" to another
entry.

Everything and its brother in Python is an object, and things like lists and
dicts reference objects automagically.
I'd like to refer to another entry and not copy that entry, i need to
know later that this is a reference to another entry, i need to find
also access that entry then.

Depending on how I read this, it is either trivial or impossible:

A name is bound to an object by assignment.
An object can have many names bound to it.
A name can, at different times, refer to different objects.
An object does not know which names are bound to it, or in what sequence it
was done.

So you can go from name to object, but not the other way around.
You can use the same name in a loop to refer to different objects, and in each
iteration, use the name to store a reference to the current object in a list
or dict.
Is something like this possible in Python?

Not too sure what you are trying to do.
The references only need to refer to entries in this structure.
The lists may change at runtime (entries removed / added), so
storing the index may not help.

You could start off with a list of lists of lists, to any level of nesting
that you want. This will give you a structure like a tree with branches and
twigs and leaves, but I am not sure if this is what you want or need, or if a
flat structure like third normal form would suffice, or if you need a
relational database.

- Hendrik
 
J

J Kenneth King

Hendrik van Rooyen said:
This sounds terribly convoluted.
What are you trying to do?


Everything and its brother in Python is an object, and things like lists and
dicts reference objects automagically.


Depending on how I read this, it is either trivial or impossible:

A name is bound to an object by assignment.
An object can have many names bound to it.
A name can, at different times, refer to different objects.
An object does not know which names are bound to it, or in what sequence it
was done.

So you can go from name to object, but not the other way around.
You can use the same name in a loop to refer to different objects, and in each
iteration, use the name to store a reference to the current object in a list
or dict.


Not too sure what you are trying to do.


You could start off with a list of lists of lists, to any level of nesting
that you want. This will give you a structure like a tree with branches and
twigs and leaves, but I am not sure if this is what you want or need, or if a
flat structure like third normal form would suffice, or if you need a
relational database.

- Hendrik

I'm not really sure what he wants either, but it sounds suspiciously
like a linked list.

In regards to the OP, though not a true linked-list, Python objects can
be built with classes that describe essentially the same functionality.
Just be careful about keeping references to large in-memory objects (see
weakref) and try to avoid circular references in your mappings.

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top