Re: tuple to string?

Discussion in 'Python' started by Robert Kern, Jul 22, 2005.

  1. Robert Kern

    Robert Kern Guest

    Francois De Serres wrote:
    > hiho,
    >
    > what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
    > the string 'spam'?


    In [1]: t = (0x73, 0x70, 0x61, 0x6D)

    In [2]: ''.join(chr(x) for x in t)
    Out[2]: 'spam'

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Jul 22, 2005
    #1
    1. Advertising

  2. On Fri, 22 Jul 2005 06:07:28 -0700, Robert Kern wrote:

    > Francois De Serres wrote:
    >> hiho,
    >>
    >> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
    >> the string 'spam'?

    >
    > In [1]: t = (0x73, 0x70, 0x61, 0x6D)
    >
    > In [2]: ''.join(chr(x) for x in t)
    > Out[2]: 'spam'


    I get a syntax error when I try that. I guess anyone who hasn't started
    using Python 2.4 will also get the same error.

    Since t is just a tuple, there isn't a big advantage as far as I can
    see to build up and dispose of the generator machinery just for grabbing
    the next item in a tuple. So a list comprehension will work just as well,
    and in older versions of Python:

    ''.join([chr(x) for x in (0x73, 0x70, 0x61, 0x6D)])

    For an even more version-independent method:

    L = []
    for n in (0x73, 0x70, 0x61, 0x6D):
    L.append(chr(n))
    print ''.join(L)


    or even:

    >>> ''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))

    'spam'



    --
    Steven.
     
    Steven D'Aprano, Jul 23, 2005
    #2
    1. Advertising

  3. Steven D'Aprano wrote:
    > On Fri, 22 Jul 2005 06:07:28 -0700, Robert Kern wrote:
    > ... or even:
    >
    >>>>''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))

    >
    > 'spam'


    This is exactly what is wrong with lambda. It yearns for over-use.
    This last should be:

    >>>''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))


    --Scott David Daniels
     
    Scott David Daniels, Jul 23, 2005
    #3
  4. Robert Kern

    John Machin Guest

    Steven D'Aprano wrote:

    >
    >
    >>>>''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))

    >
    > 'spam'


    Why the verbal diarrhoea? What's wrong with the (already posted)

    ''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))

    ???
     
    John Machin, Jul 23, 2005
    #4
  5. On Sat, 23 Jul 2005 23:26:19 +1000, John Machin wrote:

    > Steven D'Aprano wrote:
    >
    >>
    >>
    >>>>>''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))

    >>
    >> 'spam'

    >
    > Why the verbal diarrhoea?


    One line is hardly verbal diarrhoea.

    > What's wrong with the (already posted)
    >
    > ''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))
    >
    > ???


    Nothing.

    If I had seen the already posted solution using chr on its own without
    lambda, I wouldn't have bothered posting the lambda solution. But I
    didn't, so I did.

    As another poster has already pointed out, lambda cries out for over-use,
    and this was a perfect example of it.


    --
    Steven.
     
    Steven D'Aprano, Jul 24, 2005
    #5
  6. Robert Kern

    John Machin Guest

    Steven D'Aprano wrote:
    > On Sat, 23 Jul 2005 23:26:19 +1000, John Machin wrote:
    >
    >
    >>Steven D'Aprano wrote:
    >>
    >>
    >>>
    >>>>>>''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))
    >>>
    >>>'spam'

    >>
    >>Why the verbal diarrhoea?

    >
    >
    > One line is hardly verbal diarrhoea.
    >
    >
    >>What's wrong with the (already posted)
    >>
    >>''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))
    >>
    >>???

    >
    >
    > Nothing.
    >
    > If I had seen the already posted solution using chr on its own without
    > lambda, I wouldn't have bothered posting the lambda solution. But I
    > didn't, so I did.
    >
    > As another poster has already pointed out, lambda cries out for over-use,
    > and this was a perfect example of it.


    Here are a couple of reductions you can use in future, in the order given:

    (1)
    lambda <args>: foo(<args>) -> foo # for *any* function foo, not just chr

    (2)
    lambda <args>: <almost_any_guff>
    ->
    def meaningful_func_name(<args>):
    <almost_any_guff>
     
    John Machin, Jul 24, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    961
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    616
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    829
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    971
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page