Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > serial iteration over several lists

Reply
Thread Tools

serial iteration over several lists

 
 
Martin DeMello
Guest
Posts: n/a
 
      08-21-2004
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
 
Reply With Quote
 
 
 
 
Brandon
Guest
Posts: n/a
 
      08-22-2004
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 <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      08-22-2004
Brandon <(E-Mail Removed)> 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
 
Reply With Quote
 
Jeffrey Froman
Guest
Posts: n/a
 
      08-22-2004
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

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      08-23-2004
Jeffrey Froman <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to iterate over several lists? kj Python 6 06-06-2009 02:19 AM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Iterating over several lists at once Gal Diskin Python 17 12-27-2006 07:45 PM
Iteration over Lists and Strings DeepBleu Python 10 08-30-2004 07:05 AM



Advertisments