Converting Nested Dictionary to a custom Hierarchy Tree

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

Converting Nested Dictionary to a custom Hierarchy Tree

Postby anhsirk » Tue Jun 11, 2013 10:06 pm

Hi,
Am new to python and need help with nested dictionaries and recursion

I have a list
Code: Select all
X = [['A', 'A1', 'A11'], ['A', 'A1', 'A12'],['A', 'A2', 'A21'],['A', 'A2', 'A22'],
         ['B', 'B1', 'B11'], ['B', 'B1', 'B12'],['B', 'B2', 'A21'],
         ['C', 'C1'],
        ]


I converted it to a nested dictionary
Code: Select all
# Convert List to Nested Dict
    d = {}
    for path in sorted(X):
        current_level = d       
        for part in path:
            if part not in current_level:
                current_level[part] = {}
            current_level = current_level[part]
    print d


Now, I need to manipulate this to fit my API for hierarchy builder. The required output is something like this where I can manipulate the level at which I want the folders to be checked/open. In the case below - I need to uncheck the nodes at level 3

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


I tried to achieve this to a certain extent with the code below, but I can't seem to get the depth of the dictionary to manipulate

Code: Select all
# Nested Dict Converted to Tree Dict
    def convert(d):
        if not isinstance(d, dict):
            return [{'nm' : item, 'chk' : False} for item in d]
        else:
            return [{
                'nm' : k, 'chk' : True,                 
                'i' : convert(v)
                } for k, v in sorted(d.iteritems())]


Any pointers will be greatly appreciated
anhsirk
 
Posts: 5
Joined: Tue Jun 11, 2013 9:34 pm

Re: Converting Nested Dictionary to a custom Hierarchy Tree

Postby ochichinyezaboombwa » Wed Jun 12, 2013 5:44 am

After
I have a list:
Code: Select all
X = [['A', 'A1', 'A11'],....
it would be best if you were to post the expected output. I don't see any nested dictionary or a "custom hierarchy tree" or anything requiring recursion here.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Converting Nested Dictionary to a custom Hierarchy Tree

Postby anhsirk » Wed Jun 12, 2013 11:49 am

The expected output is in the post - the 3rd code snippet.
Code: Select all
[   {'i': [
      {'i': [   {'i': [], 'chk': False, 'nm': 'A11'},
               {'i': [], 'chk': False, 'nm': 'A12'}],
      'chk': True, 'nm': 'A1'},
      {'i': [   {'i': [], 'chk': False, 'nm': 'A21'},
            {'i': [], 'chk': False, 'nm': 'A22'}],
      'chk': True, 'nm': 'A2'}],
   'chk': True, 'nm': 'A'},
   {'i': [   
      {'i': [   {'i': [], 'chk': False, 'nm': 'B11'},
            {'i': [], 'chk': False, 'nm': 'B12'}],
      'chk': True, 'nm': 'B1'},
      {'i': [   {'i': [], 'chk': False, 'nm': 'A21'}],
      'chk': True, 'nm': 'B2'}],
   'chk': True, 'nm': 'B'},
   {'i': [
      {'i': [], 'chk': True, 'nm': 'C1'}],
   'chk': True, 'nm': 'C'}]   
anhsirk
 
Posts: 5
Joined: Tue Jun 11, 2013 9:34 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 27 guests