Velocity Reviews > Re: tuple to string?

Re: tuple to string?

Robert Kern
Guest
Posts: n/a

 07-22-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Steven D'Aprano
Guest
Posts: n/a

 07-23-2005
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.

Scott David Daniels
Guest
Posts: n/a

 07-23-2005
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
(E-Mail Removed)

John Machin
Guest
Posts: n/a

 07-23-2005
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)))

???

Steven D'Aprano
Guest
Posts: n/a

 07-24-2005
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.

John Machin
Guest
Posts: n/a

 07-24-2005
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>

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Steve Python 1 12-13-2005 10:25 PM Gregor Horvath Python 37 03-30-2005 06:58 AM Michal Mikolajczyk Python 1 04-20-2004 08:37 PM Jeff Epler Python 0 04-20-2004 03:36 PM Bill Scherer Python 0 04-20-2004 03:34 PM