[Numpy-discussion] array constructor from generators?

Zachary Pincus zpincus at stanford.edu
Tue Apr 4 17:19:18 EDT 2006


Hi folks,

Sorry if this has already been discussed, but do you all think it a  
good idea to extend the array constructor so that it can accept  
generators instead of lists?

I often construct arrays from list comprehensions on generators, e.g.  
to read a tab-delimited file in:
numpy.array([map(float, line.split()) for line in file])
or making an array of pairs of numbers:
numpy.array([f for f in unique_combinations(input, 2)])

If the array constructor accepted generators (and turned them into  
lists behind the scenes, or even evaluated them lazily while filling  
in the memory buffer, not sure what would be more efficient), the  
above could be written somewhat more cleanly:
numpy.array(map(float, line.split() for line in file) (using a  
generator expression)
and
numpy.array(unique_combinations(input, 2))

the latter is especially a win.

Moreover, it's becoming more standard for any python thing that can  
accept a list to also accept a generator.

The downside is that currently, passing array() an object makes a 0-d  
object array with that object. If this were changed, then passing  
array() an iterator object would be handled differently than passing  
array any other object. This might possibly be a fatal flaw in this  
idea.

I'd be happy to look in to implementing this functionality if people  
think it is a good idea, and could give me some tips as to the best  
way to implement it.

Zach






More information about the NumPy-Discussion mailing list