473,703 Members | 2,355 Online

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 1106
David Isaac wrote:
>>>y
[116, 114, 121, 32, 116, 104, 105, 115]
>>>z=''.join(ch r(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
an underlying truth."
-- Umberto Eco

Oct 25 '06 #2
"David Isaac" <ai*****@verizo n.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(ch r(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,d type='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 thread has been closed and replies have been disabled. Please start a new discussion.