Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > idea on how to get/set nested python dictionary values

Reply
Thread Tools

idea on how to get/set nested python dictionary values

 
 
jojoba
Guest
Posts: n/a
 
      08-15-2006
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[i]]
else:
Data[ParamList[i]] = 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[i]]
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

 
Reply With Quote
 
 
 
 
jojoba
Guest
Posts: n/a
 
      08-15-2006
....

 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      08-15-2006

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[i]]
> else:
> Data[ParamList[i]] = 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[i]]
> 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}}}

 
Reply With Quote
 
jojoba
Guest
Posts: n/a
 
      08-15-2006
hey thank you so much!
that should work fantastically
i like your method much better
more elegant
thanks!

jojoba
=)

 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      08-15-2006
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

 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      08-15-2006
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

 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      08-15-2006
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...

 
Reply With Quote
 
jojoba
Guest
Posts: n/a
 
      08-16-2006
Once again,
Thanks to all!!!!
I did not expect to receive such a response.
Very very helpful indeed,

jojoba

o(-_-)o

 
Reply With Quote
 
metaperl
Guest
Posts: n/a
 
      08-16-2006

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

 
Reply With Quote
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      08-16-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How find all childrens values of a nested dictionary, fast! macm Python 8 11-06-2010 05:51 PM
Re: App idea, Any idea on implementation? Matthew_WARREN@bnpparibas.com Python 0 02-05-2008 05:50 PM
App idea, Any idea on implementation? Dr Mephesto Python 3 02-05-2008 06:55 AM
Nested conditional expressions ---- good idea/bad idea? nimmi_srivastav@yahoo.com C Programming 10 02-02-2005 10:51 PM



Advertisments