[PYTHON MATRIX-SIG] arrays from text?

Michael McLay mclay@nist.gov
Fri, 25 Oct 1996 04:48:38 -0400


Konrad Hinsen writes:
 > > Is there a way to (quickly) generate an array of numbers from an array of  
 > > their string representations?  More specifically, if I have an array a of Char  
 > > in which each row contains a printed representation of a Float, is there a  
 > > faster way to get an array of Floats than the following?
 > > 
 > > array(map(lambda b: string.atof(b.tostring()), a))
 > 
 > Nothing I know of, and even the map() solution get more complicated
 > for arrays of higher rank. I do have a simple module that reads
 > rank-1 and rank-2 arrays from files (and reads them), but in terms
 > of efficiency it's even worse. I have decided more than once to do
 > a C module, but of course I never found the time.
 > 
 > > Even better would be the ability to use the equivalent of FORTRAN format  
 > > statements and implicit loops to read in several arrays of different types  
 > > simultaneously from either a Char array or a file.  Anyone know how hard this  
 > > would be to do?
 > 
 > Not too hard, but still a lot of work. Especially since I want such
 > a module to be able to read all reasonable representations for numbers,
 > including Fortran double precision (with a D instead of an E) and
 > Mathematica format (e.g. 2.5*10^3).

How about ftp://ftp.python.org/pub/python/contrib/Misc/sscanfmodule.c.  
It probably needs to be modified to read in arrays.  I think it just
returns tuples that correspond to the parse string.  


 > > Of course, the reason for doing this is that with array operations as fast as  
 > > they are, the bottleneck in many programs will be the time needed to read in  
 > > text files, which won't be any faster.
 > 
 > And that is exactly my observation in practice. I/O is frustratingly
 > slow!
 > 
 > Konrad.
 > -- 
 > -------------------------------------------------------------------------------
 > Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
 > Laboratoire de Dynamique Moleculaire   | Tel.: +33-76.88.99.28
 > Institut de Biologie Structurale       | Fax:  +33-76.88.54.94
 > 41, av. des Martyrs                    | Deutsch/Esperanto/English/
 > 38027 Grenoble Cedex 1, France         | Nederlands/Francais
 > -------------------------------------------------------------------------------
 > 
 > =================
 > MATRIX-SIG  - SIG on Matrix Math for Python
 > 
 > send messages to: matrix-sig@python.org
 > administrivia to: matrix-sig-request@python.org
 > =================

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================