Posted By

magicrebirth on 11/10/10


Tagged

list tree dict


Versions (?)

From a list containing tree info, generare a tree in dict format


 / Published in: Python
 

URL: http://markmail.org/message/lcay3kerfvqlhkr2#query:python%20create%20tree%20from%20list+page:1+mid:pnheuqqy64vqhkjg+state:results

The resulting tree is represented through a dict where each father gets a key (with all the children as args)

  1. source_list =[
  2. {'title': 'Project', 'parent':'root'},
  3. {'title': 'Geometry', 'parent':'root'},
  4. {'title': 'Soil', 'parent':'root'},
  5. {'title': 'Geometries', 'parent':'Project'},
  6. {'title': 'Verticals', 'parent':'Project'},
  7. {'title': 'Points', 'parent':'Geometry'},
  8. {'title': 'Layers', 'parent':'Geometry'},
  9. {'title': 'Water', 'parent':'Geometry'},
  10. {'title': 'Soiltypes', 'parent':'Soil'}]
  11.  
  12.  
  13. imd=dict()
  14. for d in source_list:
  15. par=d['parent']
  16. v=d['title']
  17. try:
  18. imd[par].append(v)
  19. except:
  20. imd[par]=[v]
  21.  
  22.  
  23.  
  24. imd
  25. {'Project': ['Geometries', 'Verticals'], 'Geometry': ['Points',
  26. 'Layers', 'Water'], 'root': ['Project', 'Geometry', 'Soil'], 'Soil':
  27. ['Soiltypes']}

Report this snippet  

You need to login to post a comment.