[Numpy-discussion] printing structured arrays

Gökhan Sever gokhansever@gmail....
Fri Mar 5 16:35:44 CST 2010


On Fri, Mar 5, 2010 at 8:00 AM, Bruce Schultz <bruce.schultz@gmail.com>wrote:

>  Hi,
>
> I've just started playing with numpy and have noticed that when printing a
> structured array that the output is not nicely formatted. Is there a way to
> make the formatting look the same as it does for an unstructured array?
>
> Here an example of what I mean:
>
> data = [ (1, 2), (3, 4.1) ]
> dtype = [('x', float), ('y', float)]
> print '### ndarray'
> a = numpy.array(data)
> print a
> print '### structured array'
> a = numpy.array(data, dtype=dtype)
> print a
>
> Output is:
> ### ndarray
> [[ 1.   2. ]
>  [ 3.   4.1]]
> ### structured array
> [(1.0, 2.0) (3.0, 4.0999999999999996)]
>
>
> Thanks
> Bruce
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
I still couldn't figure out how floating point numbers look nicely on screen
in cases like yours (i.e., trying numpy.array2string()) but you can make
sure by using numpy.savetxt("file", array, fmt="%.1f") you will always have
specified precision in the written file.

-- 
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100305/d3557196/attachment.html 


More information about the NumPy-Discussion mailing list