list of lists of lists ....

Y

yomgui

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
 
F

faulkner

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
 
M

Michal Kwiatkowski

faulkner said:
ok, so, recursion is just functional programming sugar for a loop.

And a loop is a procedural programming sugar for tail recursion. :cool:

Cheers,
mk
 
B

bearophileHUGS

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top