Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you have here is a graph (probably just a tree) with connections between nodes represented by a <code>parent</code> key. This is known as an <a href="http://en.wikipedia.org/wiki/Adjacency_list" rel="nofollow">adjacency list</a>.</p> <p>You want to produce a tree structure with links to other nodes through a <code>children</code> key which is a list of other nodes.</p> <p>To convert an adjacency list into a tree, you first need to have a way to retrieve a node by its id, so the first step is to create a dict of every node, keyed by id.</p> <p>Then you have to go through the list again and add children to their parents.</p> <p>Finally, make a list of nodes without parents (the root nodes).</p> <p>Note that you don't need a recursive algorithm at all.</p> <p>We can combine some of these steps to avoid going through the list multiple times. Code below:</p> <pre><code>def nest_list(L): """Modify list of associative dicts into a graph and return roots of graph Association is via a 'parent' key indicating a corresponding 'id'. Items in the list *will be modified*. Children will be placed in a list for a 'children' key Items without children will have no 'children' key 'parent' keys will be removed. Returned list is the full list of items which are not children of any other items. """ idx = {} children = [] root = [] # first pass: index items by id # if the item has no parents, put it in the 'root' list # otherwise add it to the 'children' list which we will # process in a second pass for item in L: idx[item['id']] = item if 'parent' in item: children.append(item) else: root.append(item) # second pass: find the parents of our children for child in children: parent = idx[child['parent']] del child['parent'] try: # retrieve the list of children for this # parent childlist = parent['children'] except KeyError: # this item has received no children so far # so we need to make a 'children' key and list # for it. parent['children'] = childlist = [] # append this child to the parent's list of children childlist.append(child) #all done: return new root list return root </code></pre> <p>Code in action:</p> <pre><code>oldlist = [{ "name":"Folder 2", "id":"zRDg", "parent":"OY00", "type":"folder" }, { "name":"Folder 1", "id":"OY00", "type":"folder" }, { "name":"Folder 3", "id":"ZDE1", "type":"folder" }, { "name":"DX00025.jpg", "id":"9Xdd", "parent":"OY00", "type":"jpeg" }] expected = [{ "name":"Folder 1", "id":"OY00", "type":"folder", "children": [{ "name":"Folder 2", "id":"zRDg", "type":"folder" }, { "name":"DX00025.jpg", "id":"9Xdd", "type":"jpeg" }] }, { "name":"Folder 3", "id":"ZDE1", "type":"folder" }] from pprint import pprint newlist = nest_list(oldlist) pprint(newlist) assert newlist == expected </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload