Formatting arrays using myarrayy.tolist()

Colin J. Williams fn681 at ncf.ca
Wed Feb 6 13:51:54 EST 2008


Max Abrahams wrote:
> I've got an array that looks like this:
> 
> 
>> >> p.xv[20:25]
> 
> array([[  1.60783821e-01,   1.04174046e+01,  -1.74045566e-03,
>           6.02421398e-01,   2.16078382e+00,  -1.60783821e-02],
>        [  1.66704816e-01,   1.04390422e+01,  -1.90421758e-03,
>           5.81767402e-01,   2.16670482e+00,  -1.66704816e-02],
>        [  1.72418976e-01,   1.04607380e+01,  -2.07379715e-03,
>           5.61054649e-01,   2.17241898e+00,  -1.72418976e-02],
>        [  1.77925722e-01,   1.04824899e+01,  -2.24898715e-03,
>           5.40285230e-01,   2.17792572e+00,  -1.77925722e-02],
>        [  1.83224500e-01,   1.05042958e+01,  -2.42957975e-03,
>           5.19461244e-01,   2.18322450e+00,  -1.83224500e-02]])
> 
> I want to dump it to  a plain text file. The problem is, it unravels the 
> array. The text file will look like this
> 
> p.xv.tofile('file','\n')
> 
> 
> 10.3528613872
> -0.00128613872188
> 0.664009998909
> 2.14178590964
> -0.0141785909637
> 0.148323738585
> 10.3743121062
> -0.0014312106189
> 
> and so on.
> i want it to look like this:
> 
> 1 2 3 4 5 6
> 7 8 9 10 11 12
> 13 14 15 16 17 18
> 19 20 21 22 23 24
> 
> 
> any help would be appreciated.
> thanks.
array is for single dimension arrays.

You might look at numpy which deals with 
multidimensional arrays.

Colin W.



More information about the Python-list mailing list