How to store the reference of a dictionary element ?

Alfredo P. Ricafort alpot at mylinuxsite.com
Thu Dec 19 05:30:16 EST 2002


On Thu, 2002-12-19 at 15:46, Terry Reedy wrote:

> Note that what you have presented up to here assumes that all keys are
> unique -- that if File has subkey New, then Edit does not.  

You're right.  The keys should be unique.  I'm actually planing to use
the the path(w/o the '&') as keys.

> 'Element' is not a Python term, and therefore your subject line and

Forgive me, I'm a newbie.  

> initial post and even the line above are ambiguous.  Do you mean the
> key string 'parentKey' or the value list [attribute1,attribute2,None]?

I meant the value list.  I want the value list to be a pointer to
another 'record'(key-value pair) in the dictionary.

> above you mean 'not a copy of the value'.   If so, then the statement
> you need is
> 
> inputData['childKey'] = [attribute1, attribute2,
> inputData['parentKey']]
> 

This won't do.  If the value list of inputData['parentKey'] is changed
the inputData['childKey'] will not see the changes. 

> Structure one require two statements for each child (or grandchild)
> line.  Suppone you already have parentKey : [attr1, attr2] in input
> Data and you come to the first child line.  Then
> 
> inputData['childKey'] = [attribute1, attribute2]
> inputData['parentKey'].append[inputData['childKey']]
> 

I think this suffers the same problem I described above.

> For grandchild lines, append the grandchild attribute list to the
> child list, and so on.  When you do so, you will also be able to
> access that list via the parent list, since the child list really is
> 'contained' within the parent list.  You will end up with a set of
> trees, each represented as a list with lists, etc and a dict mapping
> keys to nodes (lists) within some tree.  To identify parents, you
> actually need a superparent so there is just one tree.
> 

This is  what I have now, which is multi-level. I am trying to get away
from that.

> # produces, after whitespace insertion
> 
>  [['/&File', 'text', 'text',
>      ['/File/&New', 'text', 'text',
>         ['/File/New/&Folder', 'text', 'text'],
>         ['/File/New/&Directory', 'text', 'text']]],
>   ['/&Edit', 'text', 'text']]

The output that I am looking for, if I were to use structure (2), would
be:

{ 
 '/File':               ['text', 'text', None],
 '/File/New':           ['text', 'text', <ptr. to '/File'>],
 '/File/New/Folder':    ['text', 'text', <ptr. to '/File/New'>],
 '/File/New/Directory': ['text', 'text', <ptr. to '/File/New'>],
 '/Edit':               ['text', 'text', None]              
}

So my problem is how to store that pointer to(<ptr. to..>). 

The structure that I am aiming for is really like a linked list.  As
long as the pointer(ptr. to) is not changed, even if the
attributes(values) are changed, the relationship will remain intact. 

Of course, I have the option to store the 'ptr. to' as string.  But
finding the parent(or children) would be cumbersome.

AL

 






More information about the Python-list mailing list