serial iteration over several lists

Discussion in 'Python' started by Martin DeMello, Aug 21, 2004.

  1. Within Blender, I have access to a list of Objects, each Object
    containing a list of Meshes and each Mesh a list of Faces. I'd like to
    implement a face iterator, with both a next() and a prev() method, such
    that if I go off the end of a face list, it goes to the first face in
    the next mesh, similarly for going off the end of the last mesh in an
    object, and similarly for the prev() iterator.

    In other words, I want to simulate appending all the faces into one long
    list, but without the overhead of actually creating the list. I can
    think of several ways to do it, but they all feel like solutions
    'translated' from some other language - is there a nice pythonic way to
    do this?

    martin
     
    Martin DeMello, Aug 21, 2004
    #1
    1. Advertising

  2. Martin DeMello

    Brandon Guest

    How about this:

    >>> import itertools
    >>> def mesh_flatten(meshes):

    .... return itertools.chain(*meshes)
    ....
    >>> meshes = [

    .... ["face1", "face2", "face3"],
    .... ["face4", "face5", "face6"],
    .... ["face7", "face8", "face9"],
    .... ["face10", "face11", "face12", "face13"],
    .... ]
    >>>
    >>> list(mesh_flatten(meshes))

    ['face1', 'face2', 'face3', 'face4', 'face5', 'face6', 'face7',
    'face8', 'face9', 'face10', 'face11', 'face12', 'face13']


    Martin DeMello <> wrote in message news:<tgOVc.176766$gE.1703@pd7tw3no>...
    > Within Blender, I have access to a list of Objects, each Object
    > containing a list of Meshes and each Mesh a list of Faces. I'd like to
    > implement a face iterator, with both a next() and a prev() method, such
    > that if I go off the end of a face list, it goes to the first face in
    > the next mesh, similarly for going off the end of the last mesh in an
    > object, and similarly for the prev() iterator.
    >
    > In other words, I want to simulate appending all the faces into one long
    > list, but without the overhead of actually creating the list. I can
    > think of several ways to do it, but they all feel like solutions
    > 'translated' from some other language - is there a nice pythonic way to
    > do this?
    >
    > martin
     
    Brandon, Aug 22, 2004
    #2
    1. Advertising

  3. Brandon <> wrote:
    > How about this:
    >
    > >>> import itertools
    > >>> def mesh_flatten(meshes):

    > ... return itertools.chain(*meshes)

    [...]

    Thanks - that looks like an excellent starting point. Has there been any
    work done on bidirectional iterators?

    martin
     
    Martin DeMello, Aug 22, 2004
    #3
  4. Martin DeMello wrote:

    > Thanks - that looks like an excellent starting point. Has there been any
    > work done on bidirectional iterators?


    You could emulate one by maintaining a counter for the current index into
    the imaginary flattened list, and iterating up to that index with each
    prev() call and just past it with each next(). This of course requires that
    you basically start from the beginning of your nested list each time
    though, and I'm not sure if it would be more or less efficient than
    indexing into a pre-flattened list in memory.

    One more suggestion -- if you use a recursive generator for your iteration,
    you can then use it with a list nested to arbitrary depth, something like:

    #############################################

    def deep_iter(nested_list):
    for x in nested_list:
    if isinstance(x, list):
    for n in deep_iter(x):
    yield n
    else:
    yield x

    #############################################


    Jeffrey
     
    Jeffrey Froman, Aug 22, 2004
    #4
  5. Jeffrey Froman <> wrote:
    > Martin DeMello wrote:
    >
    > > Thanks - that looks like an excellent starting point. Has there been any
    > > work done on bidirectional iterators?

    >
    > You could emulate one by maintaining a counter for the current index into
    > the imaginary flattened list, and iterating up to that index with each
    > prev() call and just past it with each next(). This of course requires that
    > you basically start from the beginning of your nested list each time
    > though, and I'm not sure if it would be more or less efficient than
    > indexing into a pre-flattened list in memory.


    I ended up just maintaining a few list indexes, and writing code to wrap
    them around properly - turned out to be the simplest way since
    bidirectionality was more important than iterator-like support.

    > One more suggestion -- if you use a recursive generator for your iteration,
    > you can then use it with a list nested to arbitrary depth, something like:
    >
    > #############################################
    >
    > def deep_iter(nested_list):
    > for x in nested_list:
    > if isinstance(x, list):
    > for n in deep_iter(x):
    > yield n
    > else:
    > yield x
    >
    > #############################################


    I thought of that, but again it proved nigh impossible to backtrack
    cleanly over. The other idea I had was to maintain two iterators, one
    going forwards and one going backwards, and reinitialize them as needed.
    For example, if I wanted to do that over the list [1,2,3,4,5,6,7,8,9]
    I'd set forward to 1 and reverse to 9, then iterate forward as long as
    next was called, then when prev was called, reinitialize reverse to
    wherever the index had reached and call its next method.

    martin
     
    Martin DeMello, Aug 23, 2004
    #5
    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. DeepBleu

    Iteration over Lists and Strings

    DeepBleu, Aug 28, 2004, in forum: Python
    Replies:
    10
    Views:
    444
    Alex Martelli
    Aug 30, 2004
  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:
    409
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Gal Diskin

    Iterating over several lists at once

    Gal Diskin, Dec 13, 2006, in forum: Python
    Replies:
    17
    Views:
    458
    Roy Smith
    Dec 27, 2006
  4. Rudi
    Replies:
    5
    Views:
    5,023
  5. kj
    Replies:
    6
    Views:
    330
    Steven D'Aprano
    Jun 6, 2009
Loading...

Share This Page