446,231 Members | 1,636 Online
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

# list of lists of lists ....

 P: n/a Hi, I have a list of data (type A) my list can includes element of type A or a lists, these list can includes element of type A or a lists, and so on ... is there a simple way to obtain a single list of all the elemets of type A ? thanks yomgui Jul 28 '06 #1
6 Replies

 P: n/a I forgot the most important, I am looking for a non recursive method. thanks yomgui yomgui wrote: > Hi, I have a list of data (type A) my list can includes element of type A or a lists, these list can includes element of type A or a lists, and so on ... is there a simple way to obtain a single list of all the elemets of type A ? thanks yomgui Jul 28 '06 #2

 P: n/a recursion. def get_As(L): res = [] for elem in L: if isinstance(elem, A): res.append(elem) elif isinstance(elem, list): res += get_As(elem) return res i also have a Tree class in my rc: http://home.comcast.net/~faulkner612...on/pythonrc.py yomgui wrote: Hi, I have a list of data (type A) my list can includes element of type A or a lists, these list can includes element of type A or a lists, and so on ... is there a simple way to obtain a single list of all the elemets of type A ? thanks yomgui Jul 28 '06 #3

 P: n/a doh. ok, so, recursion is just functional programming sugar for a loop. def get_As(L): checking = [elem for elem in L if isinstance(elem, list)] # the equivalent of elem in recursion all_As = [elem for elem in L if isinstance(elem, A)] while checking: new_checking = [] # all lists in all lists in checking for sub_L in checking: for elem in sub_L: if isinstance(elem, A): all_As.append(elem) elif isinstance(elem, list): new_checking.append(elem) checking = new_checking return all_As yomgui wrote: I forgot the most important, I am looking for a non recursive method. thanks yomgui yomgui wrote: Hi, I have a list of data (type A) my list can includes element of type A or a lists, these list can includes element of type A or a lists, and so on ... is there a simple way to obtain a single list of all the elemets of type A ? thanks yomgui Jul 28 '06 #4

 P: n/a faulkner wrote: ok, so, recursion is just functional programming sugar for a loop. And a loop is a procedural programming sugar for tail recursion. 8-) Cheers, mk -- . o . > http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgiveness for being right. Jul 28 '06 #5

 P: n/a You can use this, fast, gives a tuple: from Tkinter import _flatten as flatten ----------------------- The xflatten/flatten version I sometimes use, maybe I can put something similar in the cookbook, but it can be improved a lot (and isrecursive is too much fragile): from pprint import isrecursive def xflatten(seq, tuplestoo=True, safe=True): """xflatten(seq, tuplestoo=True, safe=False): Flattens a sequence, giving an iterabile. If tupledtoo=True then it flattens tuples too. safe=True enables the recursive reference check, do not use it for too much nested structures. Examples (iterators): xflatten( "a" ) ==['a'] xflatten( [] ) ==[] xflatten( [[1,[2,[],"a"]]] ) ==[1,2,'a'] xflatten( [()] ) ==[] xflatten( ([[1,[2,[],"a", ()]]],) ) ==[1, 2, 'a'] xflatten( (12, 34, (11,)) ) ==(12, 34, 11) xflatten( (12, 34, (11,)), False ) ==[(12, 34, (11,))] Notes on speed: tuple(xflatten()) is much slower than list(xflatten()). tuplestoo=False makes this function faster. safe=True makes this function slower.""" # Modified from: http://google.com/groups?th=957cfbd2e46ac001 if safe and isrecursive(seq): raise TypeError, "given structure contains a recursive reference." if tuplestoo: if seq.__class__ not in (list, tuple): yield seq else: stack = [iter(seq)] while stack: for item in stack[-1]: if item.__class__ in (list, tuple): stack.append(iter(item)) break yield item else: stack.pop() else: if not seq.__class__ is list: yield seq else: stack = [iter(seq)] while stack: for item in stack[-1]: if item.__class__ is list: stack.append(iter(item)) break yield item else: stack.pop() def flatten(seq, tuplestoo=True, safe=True): # Do not use tuple(xflatten(...)), it's slow. return list(xflatten(seq, tuplestoo, safe)) Bye, bearophile Jul 28 '06 #6

 P: n/a thanks for all your answers yomgui yomgui wrote: > Hi, I have a list of data (type A) my list can includes element of type A or a lists, these list can includes element of type A or a lists, and so on ... is there a simple way to obtain a single list of all the elemets of type A ? thanks yomgui Jul 31 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.