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


/ Published in: Python
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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']}

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.