Python arrays and sting formatting options

Grant Edwards grante at visi.com
Tue Sep 30 11:57:19 EDT 2008


On 2008-09-30, Peter Pearson <ppearson at nowhere.invalid> wrote:
> On Tue, 30 Sep 2008 00:04:18 +0200, Ivan Rebori wrote:
>>
>> 1. Multi dimensional arrays - how do you load them in python
>> For example, if I had:
>> -------
>> 1 2 3
>> 4 5 6
>> 7 8 9
>>
>> 10 11 12
>> 13 14 15
>> 16 17 18
>> -------
>> with "i" being the row number, "j" the column number, and "k" the ..
>> uhmm, well, the "group" number, how would you load this ?
>>
>> If fortran90 you would just do:
>>
>> do 10 k=1,2
>> do 20 i=1,3
>>
>> read(*,*)(a(i,j,k),j=1,3)
>>
>> 20 continue
>> 10 continue
>>
>> How would the python equivalent go ?

You would drag yourself out of the 1960s, install numpy, and
then do something like this:

   a = read_array(open("filename.dat","r"))

> Since you're coming from the FORTRAN world (thank you for that
> stroll down Memory Lane), you might be doing scientific
> computations, and so might be interested in the SciPy package
> (Google scipy), which gives you arrays and matrices. Don't
> expect to be able to use it without learning some Python,
> though.

If not full-up scipy (which provides all sorts of scientific
and numerical-analysis stuff), then at least numpy (which
provides the basic array/matrix operations:

  http://numpy.scipy.org/

Though the software is free, the documentation isn't.  You've
got to buy the book if you want something to read.  IMO, it's
definitely worth it, and a good way to support the project even
if you don't really need something to keep your bookends apart.
  
Scientific Python is something else the OP might be interested
in. Yes, Scientific Python is different than SciPy:

http://dirac.cnrs-orleans.fr/plone/software/scientificpython/overview/

If you're a Windows user, I can recommend the Enthough Python
distribution.  It has all sorts of numerical and scientific
"batteries included".

http://www.enthought.com/products/epd.php

It includes both scipy and scientific python as well as several
options for data visualization (e.g. matplotlib, VTK).

There's also an Enthought Python distro for Linux, but I've
never tried it.  I run Gentoo Linux, and there are standard
ebuilds for pretty much all of the stuff in EPD.

-- 
Grant Edwards                   grante             Yow! I've read SEVEN
                                  at               MILLION books!!
                               visi.com            



More information about the Python-list mailing list