[Numpy-discussion] Casting a float array into a string array

Matthieu Brucher matthieu.brucher@gmail....
Fri Oct 5 09:19:00 CDT 2007


I'd like to have the '2.', because if the number is negative, only '-' is
returned, not the real value.

Matthieu

2007/10/5, lorenzo bolla <lbolla@gmail.com>:
>
> what's wrong with astype?
>
> In [3]: x = numpy.array([[2.,3.],[4.,5.]])
>
> In [4]: x.astype(str)
> Out[4]:
> array([['2', '3'],
>        ['4', '5']],
>       dtype='|S1')
>
> and if you want a list:
>
> In [5]: x.astype(str).tolist()
> Out[5]: [['2', '3'], ['4', '5']]
>
>
> L.
>
>
> On 10/5/07, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
>
> > Hi,
> >
> > I'm trying to cast a float array into a string array (for instance
> > transforming [[2., 3.], [4., 5.]] into [['2.', '3.'], ['4.', '5.']]), I
> > tried with astype(str) and every variation (str_, string, string_, string0),
> > but not luck.
> > Is there a function or a method of the array class that can fulfill my
> > needs ? And if there is a way to add a formatting option ('1.1f' for
> > instance), it would be even better.
> >
> > Matthieu
> >
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion@scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
> >
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20071005/ea705ae8/attachment.html 


More information about the Numpy-discussion mailing list