469,361 Members | 2,336 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,361 developers. It's quick & easy.

numbers to string

>>y
[116, 114, 121, 32, 116, 104, 105, 115]
>>z=''.join(chr(yi) for yi in y)
z
'try this'

What is an efficient way to do this if y is much longer?
(A numpy solution is fine.)

Thanks,
Alan Isaac
Oct 25 '06 #1
4 1042
David Isaac wrote:
>>>y
[116, 114, 121, 32, 116, 104, 105, 115]
>>>z=''.join(chr(yi) for yi in y)
z
'try this'

What is an efficient way to do this if y is much longer?
(A numpy solution is fine.)
With numpy, something like the following:
>>from numpy import *
y = [116, 114, 121, 32, 116, 104, 105, 115]
a = array(y, dtype=uint8)
z = a.tostring()
z
'try this'

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Oct 25 '06 #2
"David Isaac" <ai*****@verizon.netwrites:
>y
[116, 114, 121, 32, 116, 104, 105, 115]
>z=''.join(chr(yi) for yi in y)
z
'try this'

What is an efficient way to do this if y is much longer?
import array
z = array.array('B',y).tostring()
Oct 25 '06 #3
David Isaac wrote:
>>>y
[116, 114, 121, 32, 116, 104, 105, 115]
>>>z=''.join(chr(yi) for yi in y)
z
'try this'

What is an efficient way to do this if y is much longer?
(A numpy solution is fine.)
Here's another numpy solution just for fun:

import numpy
z = numpy.array(y,dtype='u1').view('S%d' % len(y))[0]
-Travis

Oct 25 '06 #4
Robert Kern wrote:
>>from numpy import *
y = [116, 114, 121, 32, 116, 104, 105, 115]
a = array(y, dtype=uint8)
z = a.tostring()
z
'try this'

Very nice! Thanks also to Paul and Travis!
Alan Isaac
Oct 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Eric Lilja | last post: by
3 posts views Thread by Stewart Allen | last post: by
2 posts views Thread by pesso | last post: by
4 posts views Thread by Tony WONG | last post: by
5 posts views Thread by lim4801 | last post: by
9 posts views Thread by =?Utf-8?B?ZGg=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.