Created
          July 28, 2013 20:32 
        
      - 
      
- 
        Save gulnara/6100119 to your computer and use it in GitHub Desktop. 
    nested dictionary solution
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import pprint | |
| def nested_tree(edges): | |
| nested = {} | |
| for i in edges: | |
| key = i[0] | |
| value = {} | |
| temp = i[1] | |
| if nested.get(key): | |
| nested[key][temp]={} | |
| else: | |
| nested[key] = value | |
| value[temp]= {} | |
| pprint.pprint(nested) | |
| if __name__ == '__main__': | |
| edges = [ | |
| ['a', 'b'], | |
| ['a', 'f'], | |
| ['a', 'j'], | |
| ['b', 'c'], | |
| ['b', 'd'], | |
| ['d', 'e'], | |
| ['f', 'g'], | |
| ['g', 'h'], | |
| ['g', 'i'], | |
| ] | |
| nested_tree(edges) | |
| # nested = { | |
| # 'a': { | |
| # 'b': { | |
| # 'c': { }, | |
| # 'd': { | |
| # 'e': { } | |
| # } | |
| # }, | |
| # 'f': { | |
| # 'g': { | |
| # 'h': { }, | |
| # 'i': { }, | |
| # }, | |
| # }, | |
| # 'j': { }, | |
| # } | |
| # } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment