idea on how to get/set nested python dictionary values

Discussion in 'Python' started by jojoba, Aug 15, 2006.

  1. jojoba

    jojoba Guest

    hello!

    i am trying to come up with a simple way to access my values in my
    nested python dictionaries

    here is what i have so far, but i wanted to run it by the geniuses out
    there who might see any probems with this...
    here is an example:

    +++++++++++++++++++++++++++++++++++++++
    def SetNewDataParam(Data, paramPath, NewData):
    ParamList = paramPath.split('/')
    numParams = len(ParamList)
    for i in range(0, numParams):
    if i != (numParams-1):
    Data = Data[ParamList]
    else:
    Data[ParamList] = NewData


    Data = {'a':{'b':{'c':1}}}
    paramPath = 'a/b/c'
    NewData = 666
    SetNewDataParam(Data, paramPath, NewData)
    +++++++++++++++++++++++++++++++++++++++


    so it works!
    when i do:
    print Data, i get
    {'a':{'b':{'c':666}}}


    but i am hesistant to be throwing around dictionary references
    how is this working????
    shouldn't my line above:
    Data = Data[ParamList]
    screw up my original Data dictionary

    Thanks to anyone with comments on this
    By the way, i love the idea of using tuples as keys, but my code is so
    far along that i dont wanna switch to that elegant way (maybe on a
    future project!)
    take care,
    jojoba
    jojoba, Aug 15, 2006
    #1
    1. Advertising

  2. jojoba

    jojoba Guest

    ....
    jojoba, Aug 15, 2006
    #2
    1. Advertising

  3. jojoba

    Guest

    jojoba wrote:
    > hello!
    >
    > i am trying to come up with a simple way to access my values in my
    > nested python dictionaries
    >
    > here is what i have so far, but i wanted to run it by the geniuses out
    > there who might see any probems with this...
    > here is an example:
    >
    > +++++++++++++++++++++++++++++++++++++++
    > def SetNewDataParam(Data, paramPath, NewData):
    > ParamList = paramPath.split('/')
    > numParams = len(ParamList)
    > for i in range(0, numParams):
    > if i != (numParams-1):
    > Data = Data[ParamList]
    > else:
    > Data[ParamList] = NewData
    >


    when I add here
    ret Data
    >
    > Data = {'a':{'b':{'c':1}}}
    > paramPath = 'a/b/c'
    > NewData = 666
    > SetNewDataParam(Data, paramPath, NewData)


    and change this to
    ret = SetNewDataParam(Data, paramPath, NewData)
    print ret
    the shell returns me
    {'c': 666}

    > +++++++++++++++++++++++++++++++++++++++
    >
    >
    > so it works!
    > when i do:
    > print Data, i get
    > {'a':{'b':{'c':666}}}
    >
    >
    > but i am hesistant to be throwing around dictionary references
    > how is this working????
    > shouldn't my line above:
    > Data = Data[ParamList]
    > screw up my original Data dictionary
    >
    > Thanks to anyone with comments on this
    > By the way, i love the idea of using tuples as keys, but my code is so
    > far along that i dont wanna switch to that elegant way (maybe on a
    > future project!)
    > take care,
    > jojoba


    | would use a recursive approach for this - given that you have a sort
    of recursive datastructure:

    py> def SetNewDataParam2(Data, NewData):
    .... if type(Data[Data.keys()[0]]) == type(dict()):
    .... SetNewDataParam2(Data[Data.keys()[0]], NewData)
    .... else:
    .... Data[Data.keys()[0]] = NewData
    ....
    .... return Data
    py> Data = {'a':{'b':{'c':1}}}
    py> NewData = 666
    py> ret = SetNewDataParam2(Data, NewData)
    py> print ret
    {'a': {'b': {'c': 666}}}
    , Aug 15, 2006
    #3
  4. jojoba

    jojoba Guest

    hey thank you so much!
    that should work fantastically
    i like your method much better
    more elegant
    thanks!

    jojoba
    =)
    jojoba, Aug 15, 2006
    #4
  5. jojoba

    Guest

    wrote:
    > py> def SetNewDataParam2(Data, NewData):
    > ... if type(Data[Data.keys()[0]]) == type(dict()):
    > ... SetNewDataParam2(Data[Data.keys()[0]], NewData)
    > ... else:
    > ... Data[Data.keys()[0]] = NewData
    > ...
    > ... return Data
    > py> Data = {'a':{'b':{'c':1}}}
    > py> NewData = 666
    > py> ret = SetNewDataParam2(Data, NewData)
    > py> print ret
    > {'a': {'b': {'c': 666}}}


    This looks better:

    def setNested(nest, val):
    el = nest.iterkeys().next()
    if isinstance(nest[el], dict):
    setNested(nest[el], val)
    else:
    nest[el] = val
    return nest

    But maybe something like this is closer to the OP needs:

    def setNested(nest, path, val):
    nest2 = nest
    for key in path[:-1]:
    nest2 = nest2[key]
    nest2[path[-1]] = val
    return nest

    ndict = {'a1':{'b1':{'c1':1}, "b2":{"c2":2}}}
    print ndict
    print setNested(ndict, ("a1", "b1", "c1"), 3)
    print setNested(ndict, ("a1", "b2", "c2"), 4)

    Output:
    {'a1': {'b1': {'c1': 1}, 'b2': {'c2': 2}}}
    {'a1': {'b1': {'c1': 3}, 'b2': {'c2': 2}}}
    {'a1': {'b1': {'c1': 3}, 'b2': {'c2': 4}}}

    (But I don't like too much that kind of in place modify.)

    Bye,
    bearophile
    , Aug 15, 2006
    #5
  6. jojoba

    Guest

    Like for the list.sort() method, to remind you that this function
    operate by side effect, maybe it's better if it doesn't return the
    modified nested dict:

    def setNested(nest, path, val):
    nest2 = nest
    for key in path[:-1]:
    nest2 = nest2[key]
    nest2[path[-1]] = val

    Bye,
    bearophile
    , Aug 16, 2006
    #6
  7. jojoba

    Simon Forman Guest

    wrote:
    > | would use a recursive approach for this - given that you have a sort
    > of recursive datastructure:
    >
    > py> def SetNewDataParam2(Data, NewData):
    > ... if type(Data[Data.keys()[0]]) == type(dict()):



    Note:

    |>> type(dict()) is dict
    True

    "dict" *is* a type...
    Simon Forman, Aug 16, 2006
    #7
  8. jojoba

    jojoba Guest

    Once again,
    Thanks to all!!!!
    I did not expect to receive such a response.
    Very very helpful indeed,

    jojoba

    o(-_-)o
    jojoba, Aug 16, 2006
    #8
  9. jojoba

    metaperl Guest

    wrote:

    > | would use a recursive approach for this - given that you have a sort
    > of recursive datastructure:
    >
    > py> def SetNewDataParam2(Data, NewData):
    > ... if type(Data[Data.keys()[0]]) == type(dict()):
    > ... SetNewDataParam2(Data[Data.keys()[0]], NewData)
    > ... else:
    > ... Data[Data.keys()[0]] = NewData
    > ...


    Data[Data.keys()[0]] is used 3 times in the above code. Is there some
    way to factor out that usage? I'm just starting python but I'm always
    on the lookout for DRY :)
    metaperl, Aug 16, 2006
    #9
  10. jojoba

    Guest

    metaperl wrote:
    > wrote:
    >
    > > | would use a recursive approach for this - given that you have a sort
    > > of recursive datastructure:
    > >
    > > py> def SetNewDataParam2(Data, NewData):
    > > ... if type(Data[Data.keys()[0]]) == type(dict()):
    > > ... SetNewDataParam2(Data[Data.keys()[0]], NewData)
    > > ... else:
    > > ... Data[Data.keys()[0]] = NewData
    > > ...

    >
    > Data[Data.keys()[0]] is used 3 times in the above code. Is there some
    > way to factor out that usage? I'm just starting python but I'm always
    > on the lookout for DRY :)


    Bearophilehugs gave a much better answer than I did, it also takes away
    the need to evaluate the keys() more than one time
    , Aug 16, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    14
    Views:
    585
    Antoon Pardon
    Jul 4, 2006
  2. Replies:
    10
    Views:
    1,234
    Big K
    Feb 2, 2005
  3. Dr Mephesto

    App idea, Any idea on implementation?

    Dr Mephesto, Feb 4, 2008, in forum: Python
    Replies:
    3
    Views:
    714
    Dennis Lee Bieber
    Feb 5, 2008
  4. Replies:
    0
    Views:
    632
  5. macm
    Replies:
    8
    Views:
    331
    Alexander Gattin
    Nov 6, 2010
Loading...

Share This Page