[Numpy-discussion] Transforming an array of numbers to an array of formatted strings
Is there a better way than looping to perform the following transformation? import numpy int_data = numpy.arange(1,11, dtype=int) # just an example str_data = int_data.astype('S4') for i in xrange(len(int_data)): ... str_data[i] = 'S%03d' % int_data[i] print str_data ['S001' 'S002' 'S003' 'S004' 'S005' 'S006' 'S007' 'S008' 'S009' 'S010'] That is, I want to format an array of numbers as strings. Thanks, Alex ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Transforming an array of numbers to an array of formatted strings
On Thu, 13 Mar 2008, Alexander Michael apparently wrote: I want to format an array of numbers as strings. To what end? Note that tofile has a format option. And for 1d array ``x`` you can always do:: strdata = list( fmt%xi for xi in x) Nice because the counter name does not bleed into your program. Cheers, Alan Isaac ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Transforming an array of numbers to an array of formatted strings
['S%03d'%i for i in int_data] David 2008/3/13, Alan G Isaac [EMAIL PROTECTED]: On Thu, 13 Mar 2008, Alexander Michael apparently wrote: I want to format an array of numbers as strings. To what end? Note that tofile has a format option. And for 1d array ``x`` you can always do:: strdata = list( fmt%xi for xi in x) Nice because the counter name does not bleed into your program. Cheers, Alan Isaac ___ 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
Re: [Numpy-discussion] Transforming an array of numbers to an array of formatted strings
2008/3/13, Alan G Isaac [EMAIL PROTECTED]: strdata = list( fmt%xi for xi in x) Nice because the counter name does not bleed into your program. On Thu, 13 Mar 2008, David Huard apparently wrote: ['S%03d'%i for i in int_data] The difference is that the counter bleeds from the list comprehension. I find that obnoxious. Cheers, Alan Isaac ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Transforming an array of numbers to an array of formatted strings
On Thu, Mar 13, 2008 at 9:49 AM, Alan G Isaac [EMAIL PROTECTED] wrote: And for 1d array ``x`` you can always do:: strdata = list( fmt%xi for xi in x) Nice because the counter name does not bleed into your program. On Thu, Mar 13, 2008 at 3:07 PM, David Huard [EMAIL PROTECTED] wrote: ['S%03d'%i for i in int_data] Thanks for the suggestions! I wasn't sure if there was a magic numpy method to do the loop quickly (as the destination array is created beforehand) without creating a temporary Python list, but I guess not. The generator/list-comprehension is likely better than my prototype. Regards, Alex ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion