Replace key value at specified depth in nested dictionary

This is the place for queries that don't fit in any of the other categories.

Replace key value at specified depth in nested dictionary

Postby anhsirk » Wed Jun 12, 2013 3:45 pm

I need to replace a key in a nested dict at any given depth. For eg.

This is the input nested Dict
Code: Select all
[   {   'chk': True,
        'i': [   {   'chk': True,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'A11'},
                              {   'chk': True, 'i': [], 'nm': 'A12'}],
                     'nm': 'A1'},
                 {   'chk': True,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'A21'},
                              {   'chk': True, 'i': [], 'nm': 'A22'}],
                     'nm': 'A2'}],
        'nm': 'A'},
    {   'chk': True,
        'i': [   {   'chk': True,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'B11'},
                              {   'chk': True, 'i': [], 'nm': 'B12'}],
                     'nm': 'B1'},
                 {   'chk': True,
                     'i': [{   'chk': True, 'i': [], 'nm': 'B21'}],
                     'nm': 'B2'}],
        'nm': 'B'},
    {   'chk': True, 'i': [{   'chk': True, 'i': [], 'nm': 'C1'}], 'nm': 'C'}]


I need to replace the chk Key at level 2 to False - ie the output should look like this

Code: Select all
[   {   'chk': True,
        'i': [   {   'chk': False,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'A11'},
                              {   'chk': True, 'i': [], 'nm': 'A12'}],
                     'nm': 'A1'},
                 {   'chk': False,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'A21'},
                              {   'chk': True, 'i': [], 'nm': 'A22'}],
                     'nm': 'A2'}],
        'nm': 'A'},
    {   'chk': True,
        'i': [   {   'chk': False,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'B11'},
                              {   'chk': True, 'i': [], 'nm': 'B12'}],
                     'nm': 'B1'},
                 {   'chk': False,
                     'i': [{   'chk': True, 'i': [], 'nm': 'B21'}],
                     'nm': 'B2'}],
        'nm': 'B'},
    {   'chk': True, 'i': [{   'chk': False, 'i': [], 'nm': 'C1'}], 'nm': 'C'}]


Any inputs will be of great help
anhsirk
 
Posts: 5
Joined: Tue Jun 11, 2013 9:34 pm

Re: Replace key value at specified depth in nested dictionar

Postby PurityLake » Wed Jun 12, 2013 5:41 pm

Your Dict looks rather odd, what exactly is a "level" in your list of dicts?
PurityLake
 
Posts: 7
Joined: Wed Jun 12, 2013 5:26 pm

Re: Replace key value at specified depth in nested dictionar

Postby anhsirk » Wed Jun 12, 2013 5:51 pm

Sorry if this was confusing. I was referring to the depth of the tree when i was referring to a level.
An easier analogy with reference to my original post is as below (A1,A2,B1... are at level 2 )

Code: Select all
|-A
|---A1
|------A11
|------A12
|---A2
|------A21
|------A22
|-B
|---B1
|------B11
|------B12
|---B2
|------B21
|-C
|---C1
anhsirk
 
Posts: 5
Joined: Tue Jun 11, 2013 9:34 pm

Re: Replace key value at specified depth in nested dictionar

Postby ochichinyezaboombwa » Wed Jun 12, 2013 7:04 pm

Just to answer your specific question:
Code: Select all
for d in your_input: # level 1
    for d2 in d['i']:# level 2
        d2['chk'] = False
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Replace key value at specified depth in nested dictionar

Postby PurityLake » Wed Jun 12, 2013 7:40 pm

I assume the key 'i' indicates a new level?
PurityLake
 
Posts: 7
Joined: Wed Jun 12, 2013 5:26 pm

Re: Replace key value at specified depth in nested dictionar

Postby anhsirk » Wed Jun 12, 2013 9:41 pm

PurityLake wrote:I assume the key 'i' indicates a new level?

'i' actually referred to items. The level is derived based on the depth of the nested dict
anhsirk
 
Posts: 5
Joined: Tue Jun 11, 2013 9:34 pm

Re: Replace key value at specified depth in nested dictionar

Postby PurityLake » Thu Jun 13, 2013 12:39 am

Code: Select all
thing = [   {   'chk': True,
        'i': [   {   'chk': True,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'A11'},
                              {   'chk': True, 'i': [], 'nm': 'A12'}],
                     'nm': 'A1'},
                 {   'chk': True,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'A21'},
                              {   'chk': True, 'i': [], 'nm': 'A22'}],
                     'nm': 'A2'}],
        'nm': 'A'},
    {   'chk': True,
        'i': [   {   'chk': True,
                     'i': [   {   'chk': True, 'i': [], 'nm': 'B11'},
                              {   'chk': True, 'i': [], 'nm': 'B12'}],
                     'nm': 'B1'},
                 {   'chk': True,
                     'i': [{   'chk': True, 'i': [], 'nm': 'B21'}],
                     'nm': 'B2'}],
        'nm': 'B'},
    {   'chk': True, 'i': [{   'chk': True, 'i': [], 'nm': 'C1'}], 'nm': 'C'}]
   
def ChangeValueByKey(list_indexes, key, value, list, lvl):
   
   d = list
   a = list[list_indexes[0]]
   prev_lvl = []
   
   if lvl == 1:
      
      a[key] = value
      list[list_indexes[0]] = a
      return
      
   #list_indexes.remove(list_indexes[0])
   prev_lvl.append(a)
   
   for i in range(0, lvl):
      
      a = a['i']
      prev_lvl.append(a)
      if i == lvl-1:
         a = a[list_indexes[-1]]
         break
      else:
         a = a[list_indexes[i+1]]
      
   a[key] = value
   #print a
   
   tmp = prev_lvl.pop()
   tmp[list_indexes[-1]] = a
   
   prev_lvl.reverse()
   
   for i in prev_lvl:
      
      tmp2 = i
      tmp2[list_indexes.pop()] = tmp
      tmp = tmp2
      
   d = tmp
      
if __name__ == "__main__":
   
   ChangeValueByKey([0, 1], "chk", False, thing, 2)


Enjoy, add to a class if you want, don't forget to add self to the expression though, do as you wish with it
PurityLake
 
Posts: 7
Joined: Wed Jun 12, 2013 5:26 pm

Re: Replace key value at specified depth in nested dictionar

Postby ochichinyezaboombwa » Thu Jun 13, 2013 5:17 am

Just for the record: my 3 lines produce exactly what was asked in the OP, and the PurityLake's 40 lines don't.
(Use pprint to see).
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Keepsake and 2 guests