list of lists of lists ....

Discussion in 'Python' started by yomgui, Jul 28, 2006.

  1. yomgui

    yomgui Guest

    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, Jul 28, 2006
    #1
    1. Advertising

  2. yomgui

    yomgui Guest

    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
    yomgui, Jul 28, 2006
    #2
    1. Advertising

  3. yomgui

    faulkner Guest

    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/programming/python/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, Jul 28, 2006
    #3
  4. yomgui

    faulkner Guest

    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
    faulkner, Jul 28, 2006
    #4
  5. faulkner wrote:
    > 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
    --
    . o . >> http://joker.linuxstuff.pl <<
    . . o It's easier to get forgiveness for being wrong
    o o o than forgiveness for being right.
    Michal Kwiatkowski, Jul 28, 2006
    #5
  6. yomgui

    Guest

    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, 2006
    #6
  7. yomgui

    yomgui Guest

    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
    yomgui, Jul 31, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Brunet
    Replies:
    7
    Views:
    423
    Greg Brunet
    Jul 2, 2003
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    404
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Damo
    Replies:
    4
    Views:
    519
  4. bahoo
    Replies:
    3
    Views:
    300
    Bruno Desthuilliers
    Apr 3, 2007
  5. antar2
    Replies:
    2
    Views:
    393
    Bighead
    Jul 17, 2008
Loading...

Share This Page