How to store the reference of a dictionary element ?

Terry Reedy tjreedy at udel.edu
Thu Dec 19 02:46:59 EST 2002


"Alfredo P. Ricafort" <alpot at mylinuxsite.com> wrote in message
news:mailman.1040272046.3141.python-list at python.org...
> I'm not exactly sure if you are really replying to this thread.

Yes, he was.

> But just in case you are, and if you curious on what I am trying to
do,
> then here it is:

This is MUCH clearer than what you wrote before.

<snip>
> inputData={'parentKey':
> [attribute1,attribute2,ptrToChild1,ptrToChild2...],
> 'child1Key':[attribute1,attribute2,ptrToGrandChild1....]
>            'child2Key':[attribute1,attribute2,None]
>            'grandchild1Key':[attribute1,attribute2,None]
>           ..}
>
>      OR
>
> (2) inputData= {'parentKey':[attribute1,attribute2,None],
>                   'child1Key':[attribute1,attribute2,ptrToParent]
>                   'child2Key':[attribute1,attribute2,ptrToParent]
>
'grandchildKey':[attribute1,attribute2,ptrToChild1]
>                  ..}

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.  I will
assume this also, but if it is not true, then the keys must be tuples
of strings instead of just one string.  If your input could include
both File/New and Edit/New, then the corresponding keys would be
('File','New') and ('Edit','New') and the example code below would be
slightly more complex.

> What I was hoping for is to store the reference(not the value) of
the
> parent or the child. So if I where to use (2) as my structure and
say:

In the implementation, names are bound to an object via a reference to
the object.  In a dictionary, a reference to the key is matched with a
reference to the value.  Access to a value via a key returns that
reference.

>    parent=inputData['child1Key'][2]
>
> then I would end up with the reference to the 'parentKey' element.

'Element' is not a Python term, and therefore your subject line and
initial post and even the line above are ambiguous.  Do you mean the
key string 'parentKey' or the value list [attribute1,attribute2,None]?
In either case, appending to the list is trivial.  I am guessing you
mean the list (or the reference thereto), so that parent[0] is the
first parent attribute.  (But if so, there will be no identifier to
identify each 'element'.)  I am also guessing that by 'not the value'
above you mean 'not a copy of the value'.   If so, then the statement
you need is

inputData['childKey'] = [attribute1, attribute2,
inputData['parentKey']]

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']]

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.

What you apparently did not know is that dict access always gets a
reference.  You would have to explicitly ask for a copy to get a copy.

> Hope this clear things up.

I believe so.  I hope the above comments and the example script below
solve your problem.

Terry J. Reedy

input = """\
/&File               text text
/File/&New           text text
/File/New/&Folder    text text
/File/New/&Directory text text
/&Edit               text text\
"""
Data = {'Menu':[]}
lines = input.split('\n')
for line in lines:
    attrs = line.split()
    keys = attrs[0][1:].split('/') # remove initial '/' before split
    if len(keys) == 1: keys.insert(0,'Menu')
    key = keys[-1][1:] # remove '&' from last key
    Data[key] = attrs
    Data[keys[-2]].append(attrs)

print Data['Menu']

# produces, after whitespace insertion

 [['/&File', 'text', 'text',
     ['/File/&New', 'text', 'text',
        ['/File/New/&Folder', 'text', 'text'],
        ['/File/New/&Directory', 'text', 'text']]],
  ['/&Edit', 'text', 'text']]





More information about the Python-list mailing list