Francois De Serres wrote:[color=blue]
> hiho,
>
> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
> the string 'spam'?[/color]
[color=blue][color=green][color=darkred]
>>> mytuple = (0x73, 0x70, 0x61, 0x6D)
>>> ''.join(chr(v) for v in mytuple)[/color][/color][/color]
'spam'
Francois De Serres wrote:[color=blue]
> hiho,
>
> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
> the string 'spam'?[/color]
one way is to use a list expression:
[color=blue][color=green][color=darkred]
>>> ''.join([chr(c) for c in (0x73, 0x70, 0x61, 0x6D)])[/color][/color][/color]
'spam'
another is to use map:
[color=blue][color=green][color=darkred]
>>> ''.join(map(chr , (0x73, 0x70, 0x61, 0x6D)))[/color][/color][/color]
'spam'
HTH,
deelan.
--
deelan, #1 fan of adriana lima!
<http://www.deelan.com/>
Francois De Serres <fdeserres@gmx. net> writes:
[color=blue]
> hiho,
>
> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
> to the string 'spam'?[/color]
..>>> t = (0x73, 0x70, 0x61, 0x6D)
..>>> ''.join('%c' % c for c in t)
'spam'
--
"Es gelten die Regeln der christlichen Seefahrt: Rot und Grün markiert
das sichere Fahrwasser, Schwarz und Gelb markieren Untiefen und
Wracks."
Christa Sager, Bundestagsfrakt ionsvorsitzende Bündnis 90/Grüne
Berthold Höllmann wrote:[color=blue]
> Francois De Serres <fdeserres@gmx. net> writes:
>[color=green]
>> hiho,
>>
>> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
>> to the string 'spam'?[/color]
>
> .>>> t = (0x73, 0x70, 0x61, 0x6D)
> .>>> ''.join('%c' % c for c in t)
> 'spam'[/color]
John Machin wrote:[color=blue]
> Reinhold Birkenfeld wrote:[color=green]
>> Berthold Höllmann wrote:
>>[color=darkred]
>>>Francois De Serres <fdeserres@gmx. net> writes:
>>>
>>>
>>>>hiho,
>>>>
>>>>what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
>>>>to the string 'spam'?
>>>
>>>.>>> t = (0x73, 0x70, 0x61, 0x6D)
>>>.>>> ''.join('%c' % c for c in t)
>>>'spam'[/color]
>>
>>
>> Or:
>>
>> t = (0x73, 0x70, 0x61, 0x6D)
>> ('%c' * len(t)) % t[/color]
>
> You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)[/color]
Ah, ok. Didn't want to lookup the precedence rules...
Reinhold Birkenfeld wrote:[color=blue]
> John Machin wrote:
>[color=green]
>>Reinhold Birkenfeld wrote:
>>[color=darkred]
>>>Berthold Höllmann wrote:
>>>
>>>
>>>>Francois De Serres <fdeserres@gmx. net> writes:
>>>>
>>>>
>>>>
>>>>>hiho,
>>>>>
>>>>>what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
>>>>>to the string 'spam'?
>>>>
>>>>.>>> t = (0x73, 0x70, 0x61, 0x6D)
>>>>.>>> ''.join('%c' % c for c in t)
>>>>'spam'
>>>
>>>
>>>Or:
>>>
>>>t = (0x73, 0x70, 0x61, 0x6D)
>>>('%c' * len(t)) % t[/color]
>>
>>You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)[/color]
>
>
> Ah, ok. Didn't want to lookup the precedence rules...[/color]
Look up the precedence rules? Are you aware of any language where * /
and % _don't_ have the same precedence??
John Machin wrote:[color=blue]
> Reinhold Birkenfeld wrote:[/color]
[color=blue][color=green]
>>Ah, ok. Didn't want to lookup the precedence rules...[/color]
>
> Look up the precedence rules? Are you aware of any language where * /
> and % _don't_ have the same precedence??[/color]
Given that % is somewhat more esoteric, I certainly have never committed
to memory its position in a precedence hierarchy of *any* language.
On Sat, 23 Jul 2005 23:31:04 +1000, John Machin wrote:
[color=blue][color=green][color=darkred]
>>>You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)[/color]
>>
>>
>> Ah, ok. Didn't want to lookup the precedence rules...[/color]
>
>
> Look up the precedence rules? Are you aware of any language where * /
> and % _don't_ have the same precedence??[/color]
Do languages like Pascal that don't have string formatting expressions, or
use the % operator, count?
How about languages like Forth that don't have precedence rules at all,
unless "first come, first served" is a precedence rule?
I'm not being academic here. I have used both these languages extensively,
admittedly many years ago.
Steven D'Aprano wrote:[color=blue]
> On Sat, 23 Jul 2005 23:31:04 +1000, John Machin wrote:
>
>[color=green][color=darkred]
>>>>You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)
>>>
>>>
>>>Ah, ok. Didn't want to lookup the precedence rules...[/color]
>>
>>
>>Look up the precedence rules? Are you aware of any language where * /
>>and % _don't_ have the same precedence??[/color]
>
>
> Do languages like Pascal that don't have string formatting expressions, or
> use the % operator, count?[/color]
A thousand pardons; I should have said "Are you aware of any language
which has % (as primarily a numeric remainder/modulo operator) but * /
and % _don't_ have the same precedence??"
OK, given a language which does have * and / used among other things for
numerical multiply and divide, (a) are you aware of any such language
which does does not have * and / at the same precedence level (b)
supposing one wanted to introduce % as a numerical
remainder/modulo/whatever operator (plus other meaning(s) for
non-numeric types), would you care to argue that it should not have the
same precedence level (as * and /)?
Pascal was/is a prime example of bad precedence choice:
a > b or c > d
means
a > (b or c) > d
in Pascal (not very useful)
and
(a > b) or (c > d)
in many other languages.
[color=blue]
>
> How about languages like Forth that don't have precedence rules at all,
> unless "first come, first served" is a precedence rule?[/color]
On Sun, 24 Jul 2005 21:55:19 +1000, John Machin wrote:
[color=blue][color=green][color=darkred]
>>>Look up the precedence rules? Are you aware of any language where * /
>>>and % _don't_ have the same precedence??[/color]
>>
>>
>> Do languages like Pascal that don't have string formatting expressions, or
>> use the % operator, count?[/color]
>
> A thousand pardons; I should have said "Are you aware of any language
> which has % (as primarily a numeric remainder/modulo operator) but * /
> and % _don't_ have the same precedence??"[/color]
[slaps head]
Ah, I had completely forgotten that Pascal has a MOD operator that is
equivalent to % and has the same precedence as * / and DIV. So scratch
Pascal off the list.
But APL uses right-to-left precedence for all operators, and Forth uses
left-to-right. There may be others.
[color=blue]
> OK, given a language which does have * and / used among other things for
> numerical multiply and divide, (a) are you aware of any such language
> which does does not have * and / at the same precedence level (b)
> supposing one wanted to introduce % as a numerical
> remainder/modulo/whatever operator (plus other meaning(s) for
> non-numeric types), would you care to argue that it should not have the
> same precedence level (as * and /)?[/color]
Yes I would.
Since the remainder (or modulo) operator is not distributive, the only
unambiguous usage is to use parentheses, or to decide on precedence rules.
The usual mathematical convention is that modulus has lower precedence
than addition, eg in "clock arithmetic" we expect that three hours after
ten is one: 10+3 modulo 12 is 1, not 13.
On Sun, 24 Jul 2005 10:39:44 -0700, Robert Kern wrote:
[color=blue]
> John Machin wrote:
>[color=green]
>> No precedence rules -> no relevance to the topic[/color]
>
> Precedence rules of other languages -> no relevance to the topic[/color]
I thought the topic was -- or at least had wandered in the direction of --
whether or not it was unthinkable for the precedence of % to be
anything but that of multiplication and division. Surely the precedence
rules of other languages have some relevance to that question.
Still, the subject is rapidly losing whatever interest it may have had.
Comment