Re: Order a list to get a hierarchical order

2012-06-08 Thread Thibaut DIRLIK
Thanks for your help, I'll test this. 2012/6/8 Peter Otten <__pete...@web.de> > Ivars Geidans wrote: > > > def append_node(n, l, ls): > > ls.append(n) > > for c in [nc for nc in l if nc.parent is n]: > > append_node(c, l, ls) > > return ls > > > > def sort_nodes(l): > > ls

Re: Order a list to get a hierarchical order

2012-06-08 Thread Peter Otten
Ivars Geidans wrote: > def append_node(n, l, ls): > ls.append(n) > for c in [nc for nc in l if nc.parent is n]: > append_node(c, l, ls) > return ls > > def sort_nodes(l): > ls = [] > for r in l: > if r.parent == None: > append_node(r, l, ls) > >

Re: Order a list to get a hierarchical order

2012-06-08 Thread Peter Otten
Ivars Geidans wrote: > Something like this? Or this (I'm reusing some of your code but let the built-in sorted() do the hard work): #!/usr/bin/env python3 import random def _reverse_iterpath(node): while node is not None: yield node.name node = node.parent def path(node):

Re: Order a list to get a hierarchical order

2012-06-08 Thread Ivars Geidans
Something like this? #!/usr/bin/env python3 import random class Node: def __init__(self, parent, name): self.parent, self.name = parent, name def __repr__(self): return self.name p_1 = Node(None, 'Parent #1') p_2 = Node(None, 'Parent #2') c_1_1 = Node(p_1, 'Child #1.1')

Order a list to get a hierarchical order

2012-06-08 Thread Thibaut DIRLIK
Hi, Having a list of objet with a parent_id attribute pointing to a parent, I want to order this list like this : [Parent #1, Child #1.1, Child#1.1.1, Child#1.1.2, Child#1.2, Parent #2, Child #2.1, ...] Any clue on how to do this ? Thanks, -- http://mail.python.org/mailman/listinfo/python-list