Velocity Reviews > list of lists of lists ....

# list of lists of lists ....

yomgui
Guest
Posts: n/a

 07-28-2006
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

yomgui
Guest
Posts: n/a

 07-28-2006
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

faulkner
Guest
Posts: n/a

 07-28-2006
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

faulkner
Guest
Posts: n/a

 07-28-2006
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

Michal Kwiatkowski
Guest
Posts: n/a

 07-28-2006
faulkner wrote:
> ok, so, recursion is just functional programming sugar for a loop.

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

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.

bearophileHUGS@lycos.com
Guest
Posts: n/a

 07-28-2006
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."""
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

yomgui
Guest
Posts: n/a

 07-31-2006

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