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 9 3173
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:
pydef 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
pyData = {'a':{'b':{'c':1}}}
pyNewData = 666
pyret = SetNewDataParam2(Data, NewData)
pyprint ret
{'a': {'b': {'c': 666}}}
hey thank you so much!
that should work fantastically
i like your method much better
more elegant
thanks!
jojoba
=) wi******@hotmail.com wrote:
pydef 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
pyData = {'a':{'b':{'c':1}}}
pyNewData = 666
pyret = SetNewDataParam2(Data, NewData)
pyprint 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
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 wi******@hotmail.com wrote:
| would use a recursive approach for this - given that you have a sort
of recursive datastructure:
pydef SetNewDataParam2(Data, NewData):
... if type(Data[Data.keys()[0]]) == type(dict()):
Note:
|>type(dict()) is dict
True
"dict" *is* a type...
Once again,
Thanks to all!!!!
I did not expect to receive such a response.
Very very helpful indeed,
jojoba
o(-_-)o wi******@hotmail.com wrote:
| would use a recursive approach for this - given that you have a sort
of recursive datastructure:
pydef 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 wrote:
wi******@hotmail.com wrote:
| would use a recursive approach for this - given that you have a sort
of recursive datastructure:
pydef 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: kbass |
last post by:
I am new to Python and I am attempting to retrieve data from a database and
I would like to place this data into a nested dictionary. After placing the
data into a dictionary, I would like to loop...
|
by: OKB (not okblacke) |
last post by:
For a variety of reasons, I'm interested in putting together some
code that will allow me to created structures out of nested classes,
something like:
class class1:
def methA(self):
print...
|
by: quadric |
last post by:
Hi,
I would like to pass a dictionary from my Python code to my Python
extension, extract
various values from the dictionary (from within the extension) , modify the
values for the
relevant...
|
by: techiepundit |
last post by:
I'm parsing some data of the form:
OuterName1 InnerName1=5,InnerName2=7,InnerName3=34;
OuterName2 InnerNameX=43,InnerNameY=67,InnerName3=21;
OuterName3 ....
and so on....
These are fake...
|
by: Jake Emerson |
last post by:
I'm attempting to build a process that helps me to evaluate the
performance of weather stations. The script below operates on an MS
Access database, brings back some data, and then loops through to...
| |
by: vatamane |
last post by:
This has been bothering me for a while. Just want to find out if it
just me or perhaps others have thought of this too: Why shouldn't the
keyset of a dictionary be represented as a set instead of a...
|
by: Rich Shepard |
last post by:
I want to code what would be nested "for" loops in C, but I don't know the
most elegant way of doing the same thing in python. So I need to learn how
from you folks. Here's what I need to do: build...
|
by: Sam Loxton |
last post by:
Hi,
I am fairly new to the python language and am trying to sort a nested
Dictionary of a Dictionary which I wish to sort by value. The dictionary
does not have to be restructured as I only need...
|
by: IamIan |
last post by:
Hello,
I'm writing a simple FTP log parser that sums file sizes as it runs. I
have a yearTotals dictionary with year keys and the monthTotals
dictionary as its values. The monthTotals dictionary...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |