[Numpy-discussion] array constructor from generators?

Christopher Barker Chris.Barker at noaa.gov
Wed Apr 5 12:07:10 CDT 2006

Zachary Pincus wrote:
> I often construct arrays from list comprehensions on generators,

 > numpy.array([map(float, line.split()) for line in file])

I know there are other uses, and this was just an example, but you can 
now do:

numpy.fromfile(file, dtype=numpy.Float, sep="\t")

Which is much faster and cleaner, if you ask me. Thanks for adding this, 

Tim Hochberg wrote:
> Without this, you probably can't do much 
> better than just building a list from the array. What would work well 
> would be to build a list, then steal its memory. 

Perhaps another option is to borrow the machinery from fromfile (see 
above), that builds an array without knowing how big it is when it 
starts. I haven't looked at the code, but I know that Travis got at 
least the idea, if not the method, from my FileScanner module I wrote a 
while back, and that dynamically allocated the memory it needed as it grew.


Christopher Barker, Ph.D.
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov

More information about the Numpy-discussion mailing list