Re: Python arrays and sting formatting options



On Tue, 30 Sep 2008 10:57:19 -0500, Grant Edwards wrote:

On 2008-09-30, Peter Pearson <ppearson@xxxxxxxxxxxxxxx> 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"))

In [64]: a = numpy.fromfile('test.txt', dtype=int, sep=' ')

In [65]: a
Out[65]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18])

In [66]: a.reshape(2, 3, 3)
Out[66]:
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],

[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])

Ciao,
Marc 'BlackJack' Rintsch
.



Relevant Pages

  • Re: Trouble using pinckle
    ... I'm new to python and i'm deelopping a small game with pygame. ... It works but when reading self, it OK inside the load ... class Prefs: ... def save: ...
    (comp.lang.python)
  • Re: Python arrays and sting formatting options
    ... Multi dimensional arrays - how do you load them in python ... package (Google scipy), which gives you arrays and matrices. ...
    (comp.lang.python)
  • Re: Factory pattern again
    ... Having overcome my first hurdle with the factory pattern, ... At the moment I'm trying to return around 2000 records from a db and load up ... I was wondering whether anyone could help me, I'm pretty new to python ... cannot work, since price is an instance attribute, not a class attribute. ...
    (comp.lang.python)
  • unable to load dco2 in python
    ... I have built DCOracle2 with python. ... the error Failed to load dco2.sl although it has been ... Oracle version: Oracle10g ...
    (comp.lang.python)
  • Python arrays and sting formatting options
    ... Multi dimensional arrays - how do you load them in python ... How do you print them with the same number of decimals? ...
    (comp.lang.python)