[Numpy-discussion] tostring() for array rows

Christopher Barker Chris.Barker at noaa.gov
Tue Oct 6 16:39:34 EDT 2009


josef.pktd at gmail.com wrote:
> If I have a structured or a regular array, is the use of strides in
> the following always correct for the length of the row memory?
> 
> I would like to do tostring() but on each row, by creating a string
> view of the memory in a 1d array.

Maybe I'm missing what you want, but why not just:

In [15]: tmp
Out[15]:
array([[ 1.07810097, -1.74157351,  0.29740878],
        [-0.16786436,  0.45752272, -0.8038045 ],
        [-0.17195028, -1.16753882,  0.04329128],
        [ 0.45460137, -0.44584955, -0.77140505]])

In [16]: rows = []

In [17]: for r in range(tmp.shape[0]):
              rows.append(tmp[r,:].tostring())
    ....:

In [19]: rows
Out[19]:
['?\xf1?\xe6\xce\x1f9\xce\xbf\xfb\xdd|.\xc85Z?\xd3\x08\xbe\xd6\xb7\xb6\xe8',
  '\xbf\xc5|\x94Sx\x92\x18?\xddH\r\\T\xfbT\xbf\xe9\xb8\xc45\xff\x92\xdf',
  '\xbf\xc6\x02w\x82\x18i\xaf\xbf\xf2\xae=/\xfe\xff\x0b?\xa6*FD\xae\xd1F',
 
'?\xdd\x180Z\xcet\xa5\xbf\xdc\x88\xcc\x8a\x8c\x8b\xe7\xbf\xe8\xafY\xa2\xf8\xac 
']


in general, you can let numpy worry about the strides, etc.

-Chris

-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (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