Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: tuple to string?

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
 
 
 
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)
 
Reply With Quote
 
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)))

???
 
Reply With Quote
 
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.

 
Reply With Quote
 
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*tuple vs tuple example print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c")) Steve Python 1 12-13-2005 10:25 PM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments