Popular recipes tagged "iterative_zip"http://code.activestate.com/recipes/tags/iterative_zip/2009-03-30T14:54:40-07:00ActiveState Code Recipesxzip - Iterative zip function for very large collections (Python)
2009-03-30T14:54:40-07:00Tucker Beckhttp://code.activestate.com/recipes/users/4169378/http://code.activestate.com/recipes/576703-xzip-iterative-zip-function-for-very-large-collect/
<p style="color: grey">
Python
recipe 576703
by <a href="/recipes/users/4169378/">Tucker Beck</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterative_zip/">iterative_zip</a>, <a href="/recipes/tags/zip/">zip</a>).
</p>
<p>The xzip function provides the same functionality as zip (python builtin), but utilizes a generator list comprehension so that zipped collections can be accessed iteratively.</p>
<p>Example:
for t in xzip( xrange( 1000000 ), xrange( 1000000, 2000000, 1 ), xrange( 888,100000000, 1 ):
print t</p>
<p>This Will begin to produce output immediately, because the collections are zipped iteratively
The output of this code is exactly equivalent to:</p>
<p>for t in zip( xrange( 1000000 ), xrange( 1000000, 2000000, 1 ), xrange( 888,100000000, 1 ):
print t</p>
<p>However, the second block (using zip) must first build the zipped collection entirely before the
for loop can iterate over it. This could take a long time.</p>
<p>Note, I used xrange here so that we don't have to wait for python to build the initial lists.
The xzip function would probably show its usefulness most if one had several huge collections that
needed to be combined iteratively.</p>
<p>I developed this function to zip long lists ( >100000 ) of vertex triples with color triples in a volume
visualizer.</p>