numbers to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Isaac

    #1

    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


  • Robert Kern

    #2
    Re: numbers to string

    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
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    • Paul Rubin

      #3
      Re: numbers to string

      "David Isaac" <aisaac0@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()

      Comment

      • Travis E. Oliphant

        #4
        Re: numbers to string

        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

        Comment

        • David Isaac

          #5
          Re: numbers to string

          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


          Comment

          Working...