Posts: 107
Threads: 45
Joined: Jun 2018
Aug-31-2018, 02:02 PM
(This post was last modified: Aug-31-2018, 02:26 PM by Larz60+.)
Could anyone tell me how to iterate over a nested dictionary and replace the keys which have underscore and are in small letters ,to keys without underscore and camelcase.
Final dictionary should have keys without underscore and should be camelcase.
the logic i wrote is
d is the dictionary...
def printDict(d):
for oldkey, v in d.iteritems():
if type(v) is dict:
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]
printDict(v)
else:
for oldkey, w in d.iteritems():
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]
def camel_case(string)
title=string.title().replace("_","")
ca,el=title[0].lower+title[1:]
Posts: 4,220
Threads: 97
Joined: Sep 2016
You have been previously asked to use python tags when posting code. See the BBCode link in my signature for instructions.
Posts: 107
Threads: 45
Joined: Jun 2018
Could anyone tell me how to iterate over a nested dictionary and replace the keys which have underscore and are in small letters ,to keys without underscore and camelcase.
Final dictionary should have keys without underscore and should be camelcase.
the logic i wrote is
d is the dictionary... def printDict(d):
for oldkey, v in d.iteritems():
if type(v) is dict:
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]
printDict(v)
else:
for oldkey, w in d.iteritems():
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]
def camel_case(string)
title=string.title().replace("_","")
ca,el=title[0].lower+title[1:]
Posts: 4,220
Threads: 97
Joined: Sep 2016
When you post the code, you need to make sure your copy/paste maintained the indentation of the code. Please reread the instructions.
That said, you are modifying the dictionary as you are iterating over it. That is a bad idea. You should build a new dictionary based on the current dictionary. If the key is appropriate, copy it's value over. If the key is not appropriate, change the key and copy the value into the new key.
Posts: 107
Threads: 45
Joined: Jun 2018
Could anyone tell me how to iterate over a nested dictionary and replace the keys which have underscore and are in small letters ,to keys without underscore and camelcase.
Final dictionary should have keys without underscore and should be camelcase.
the logic i wrote is
def printDict(d):
for oldkey, v in d.iteritems():
if type(v) is dict:
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]
printDict(v)
else:
for oldkey, w in d.iteritems():
newkey=camel_case(old_key)
d[newkey]=d.pop[oldkey]
def camel_case(string)
title=string.title().replace("_","")
camel=title[0].lower+title[1:]
Posts: 4,220
Threads: 97
Joined: Sep 2016
Have you even tried to run this code? There are errors all over it. You have no colon at the end of line 12, and you refer to oldkey two different ways. Solve the basic problems first. Get it running with a test case, if it gives the wrong output, come back and tell us exactly how the output is wrong.
Posts: 107
Threads: 45
Joined: Jun 2018
(Sep-02-2018, 05:00 PM)ichabod801 Wrote: Have you even tried to run this code? There are errors all over it. You have no colon at the end of line 12, and you refer to oldkey two different ways. Solve the basic problems first. Get it running with a test case, if it gives the wrong output, come back and tell us exactly how the output is wrong.
The code is working fine.if u have any logic for my ques ,please suggest
Posts: 8,168
Threads: 160
Joined: Sep 2016
(Sep-02-2018, 05:54 PM)saisankalpj Wrote: The code is working fine.if u have any logic for my ques ,please suggest No, it does not. As Ichabood has said there are errors.
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Sep-02-2018, 05:54 PM)saisankalpj Wrote: The code is working fine.if u have any logic for my ques ,please suggest
I did suggest, and you ignored my suggestion.
|