Iterable Flattener with Depth. - Python

This is a discussion on Iterable Flattener with Depth. - Python ; Hi, 5 minute solution to one of my requirements. I wanted to flatten iterables upto a specific depth. To be true, didn't search for it on the internet prior to writing this one. def flatten_iter(my_iter, depth=None): """my_iter can be a ...

+ Reply to Thread
Results 1 to 6 of 6

Iterable Flattener with Depth.

  1. Default Iterable Flattener with Depth.

    Hi,

    5 minute solution to one of my requirements. I wanted to flatten
    iterables upto a specific depth.
    To be true, didn't search for it on the internet prior to writing this one.


    def flatten_iter(my_iter, depth=None):
    """my_iter can be a iterable except string containing nested
    iterables upto any depth. This function will flat all
    (except string) down to a list containing all the elements in
    nested-order.
    To add to it you can specify optional depth (int or long)
    argument and the
    function will flatten the iterable upto that depth (nesting).
    """
    if not hasattr(my_iter, '__iter__') or isinstance(my_iter, basestring):
    return [my_iter]
    elif depth != None and depth <= 0:
    return my_iter
    temp = []
    for x in my_iter:
    temp.extend(flatten_iter(x, None if depth == None else depth-1))
    return temp

    py> temp = [1,[2,[3,4,5],'bash'],6,[7,[8,[9,10,['hi', 'hello']]]], 11, 12]

    py> flatten_iter(temp,1)

    [1, 2, [3, 4, 5], 'bash', 6, 7, [8, [9, 10, ['hi', 'hello']]], 11, 12]

    py> flatten_iter(temp,2)

    [1, 2, 3, 4, 5, 'bash', 6, 7, 8, [9, 10, ['hi', 'hello']], 11, 12]

    py> flatten_iter(temp)

    [1, 2, 3, 4, 5, 'bash', 6, 7, 8, 9, 10, 'hi', 'hello', 11, 12]

    py> flatten_iter(temp,3)

    [1, 2, 3, 4, 5, 'bash', 6, 7, 8, 9, 10, ['hi', 'hello'], 11, 12]



    Any comments?

    Thanks
    - Pradeep

  2. Default Re: Iterable Flattener with Depth.

    Pradeep Jindal:
    > Any comments?


    Something with similar functionality (plus another 20 utility
    functions/classes or so) has probably to go into the std lib... :-)

    Bye,
    bearophile


  3. Default Re: Iterable Flattener with Depth.

    On Nov 1, 5:03 pm, bearophileH...@lycos.com wrote:
    > Pradeep Jindal:
    >
    > > Any comments?

    >
    > Something with similar functionality (plus another 20 utility
    > functions/classes or so) has probably to go into the std lib... :-)
    >
    > Bye,
    > bearophile


    Same Here!

    - Pradeep


  4. Default Re: Iterable Flattener with Depth.

    On Nov 2, 6:32 am, praddy <praddyjin...@gmail.com> wrote:
    > On Nov 1, 5:03 pm, bearophileH...@lycos.com wrote:
    >
    > > Pradeep Jindal:

    >
    > > > Any comments?

    >
    > > Something with similar functionality (plus another 20 utility
    > > functions/classes or so) has probably to go into the std lib... :-)

    >
    > > Bye,
    > > bearophile

    >
    > Same Here!
    >
    > - Pradeep


    Yeah, everyone has to write a flatten sooner or later :-) My version
    is at:

    http://blog.tkbe.org/archive/python-flatten/

    -- bjorn


  5. Default Re: Iterable Flattener with Depth.

    thebjorn wrote:
    > On Nov 2, 6:32 am, praddy <praddyjin...@gmail.com> wrote:
    >> On Nov 1, 5:03 pm, bearophileH...@lycos.com wrote:
    >>
    >>> Pradeep Jindal:
    >>>> Any comments?
    >>> Something with similar functionality (plus another 20 utility
    >>> functions/classes or so) has probably to go into the std lib... :-)
    >>> Bye,
    >>> bearophile

    >> Same Here!
    >>
    >> - Pradeep

    >
    > Yeah, everyone has to write a flatten sooner or later :-) My version
    > is at:
    >
    > http://blog.tkbe.org/archive/python-flatten/
    >
    > -- bjorn
    >


    And here is mine. Note that it is very similar to Michael Spencer's
    implementation[1]. The only difference is that this adds a depth counter.

    def iflat(itr, depth=0):
    itr = iter(itr)
    stack = []
    cur_depth = 0

    while True:
    try:
    elem = itr.next()
    if hasattr(elem, "__iter__") and cur_depth < depth:
    stack.append(itr)
    itr = iter(elem)
    cur_depth += 1
    else:
    yield elem
    except StopIteration:
    if not stack:
    raise StopIteration
    cur_depth -= 1
    itr = stack.pop()


    if __name__ == "__main__":
    test1 = ((0, 1, 2), ((3, 4), 5), (((6, 7), 8), 9))
    test2 = [1,[2,[3,4,5],'bash'],6,[7,[8,[9,10,['hi', 'hello']]]], 11, 12]

    for x in (test1, test2):
    print
    print list(iflat(x))
    print
    print list(iflat(x, 1))
    print list(iflat(x, 2))
    print list(iflat(x, 3))
    print list(iflat(x, 4))
    print iflat(x, 10)

    Ian

    [1] http://mail.python.org/pipermail/pyt...ch/312022.html


  6. Default Re: Iterable Flattener with Depth.

    On Friday 02 Nov 2007 10:43:45 pm Ian Clark wrote:
    > thebjorn wrote:
    > > On Nov 2, 6:32 am, praddy <praddyjin...@gmail.com> wrote:
    > >> On Nov 1, 5:03 pm, bearophileH...@lycos.com wrote:
    > >>> Pradeep Jindal:
    > >>>> Any comments?
    > >>>
    > >>> Something with similar functionality (plus another 20 utility
    > >>> functions/classes or so) has probably to go into the std lib... :-)
    > >>> Bye,
    > >>> bearophile
    > >>
    > >> Same Here!
    > >>
    > >> - Pradeep

    > >
    > > Yeah, everyone has to write a flatten sooner or later :-) My version
    > > is at:
    > >
    > > http://blog.tkbe.org/archive/python-flatten/
    > >
    > > -- bjorn

    >
    > And here is mine. Note that it is very similar to Michael Spencer's
    > implementation[1]. The only difference is that this adds a depth counter.
    >
    > def iflat(itr, depth=0):
    > itr = iter(itr)
    > stack = []
    > cur_depth = 0
    >
    > while True:
    > try:
    > elem = itr.next()
    > if hasattr(elem, "__iter__") and cur_depth < depth:
    > stack.append(itr)
    > itr = iter(elem)
    > cur_depth += 1
    > else:
    > yield elem
    > except StopIteration:
    > if not stack:
    > raise StopIteration
    > cur_depth -= 1
    > itr = stack.pop()
    >
    >
    > if __name__ == "__main__":
    > test1 = ((0, 1, 2), ((3, 4), 5), (((6, 7), 8), 9))
    > test2 = [1,[2,[3,4,5],'bash'],6,[7,[8,[9,10,['hi', 'hello']]]], 11, 12]
    >
    > for x in (test1, test2):
    > print
    > print list(iflat(x))
    > print
    > print list(iflat(x, 1))
    > print list(iflat(x, 2))
    > print list(iflat(x, 3))
    > print list(iflat(x, 4))
    > print iflat(x, 10)
    >
    > Ian
    >
    > [1] http://mail.python.org/pipermail/pyt...ch/312022.html


    Very nice non-recursive (iterative) implementation of the thing with required
    features. Yours is double faster than mine if depth is not specified, Mine is
    double faster than yours if depth is specified. And my main aim was the depth
    thingy. What do you think?

    - Pradeep

+ Reply to Thread

Similar Threads

  1. Iterable arrays
    By Application Development in forum Java
    Replies: 11
    Last Post: 10-26-2007, 07:35 AM
  2. Re: gdbm objects not iterable?
    By Application Development in forum Python
    Replies: 0
    Last Post: 10-03-2007, 11:05 AM
  3. Transparency flattener issues with my printer, how can I fix it?
    By Application Development in forum Adobe Indesign
    Replies: 6
    Last Post: 06-27-2007, 04:53 PM
  4. Write Depth Map to Depth buffer
    By Application Development in forum Graphics
    Replies: 0
    Last Post: 04-16-2007, 05:00 AM
  5. [AS][CS2] A transparency flattener object in Acrobat 6 ...
    By Application Development in forum Adobe Indesign
    Replies: 1
    Last Post: 01-24-2007, 04:30 PM