tuple to string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francois De Serres

    tuple to string?

    hiho,

    what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
    the string 'spam'?

    TIA,
    Francois
  • Jason Drew

    #2
    Re: tuple to string?

    ''.join((chr(e) for e in (0x73, 0x70, 0x61, 0x6D)))

    Comment

    • Peter Hansen

      #3
      Re: tuple to string?

      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'

      Comment

      • deelan

        #4
        Re: tuple to string?

        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/>




        Comment

        • Berthold Höllmann

          #5
          Re: tuple to string?

          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

          Comment

          • Reinhold Birkenfeld

            #6
            Re: tuple to string?

            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]

            Or:

            t = (0x73, 0x70, 0x61, 0x6D)
            ('%c' * len(t)) % t

            Reinhold

            Comment

            • John Machin

              #7
              Re: tuple to string?

              Reinhold Birkenfeld wrote:[color=blue]
              > Berthold Höllmann wrote:
              >[color=green]
              >>Francois De Serres <fdeserres@gmx. net> writes:
              >>
              >>[color=darkred]
              >>>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]
              >
              >
              > 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 :-)

              Comment

              • Reinhold Birkenfeld

                #8
                Re: tuple to string?

                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

                Comment

                • John Machin

                  #9
                  Re: tuple to string?

                  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??

                  Comment

                  • Robert Kern

                    #10
                    Re: tuple to string?

                    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.

                    --
                    Robert Kern
                    rkern@ucsd.edu

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

                    Comment

                    • Steven D'Aprano

                      #11
                      Re: tuple to string?

                      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.

                      Comment

                      • John Machin

                        #12
                        Re: tuple to string?

                        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]

                        No precedence rules -> no relevance to the topic

                        Comment

                        • Steven D'Aprano

                          #13
                          Re: tuple to string?

                          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.


                          --
                          Steven.

                          Comment

                          • Robert Kern

                            #14
                            Re: tuple to string?

                            John Machin wrote:
                            [color=blue]
                            > No precedence rules -> no relevance to the topic[/color]

                            Precedence rules of other languages -> no relevance to the topic

                            --
                            Robert Kern
                            rkern@ucsd.edu

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

                            Comment

                            • Steven D'Aprano

                              #15
                              Re: tuple to string?

                              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.


                              --
                              Steven.

                              Comment

                              Working...