swapping numeric items in a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jiang Nutao

    #1

    swapping numeric items in a list

    Hi,

    I simplify my problem like below

    To convert list
    aa = [0x12, 0x34, 0x56, 0x78]
    into
    [0x34, 0x12, 0x78, 0x56]

    How to do it fast? My real list is huge.

    Thanks a lot.
    Jason



  • faulkner

    #2
    Re: swapping numeric items in a list

    for i in xrange(0, len(your_list), 2):
    your_list[i], your_list[i + 1] = your_list[i + 1], your_list[i]


    Jiang Nutao wrote:
    Hi,
    >
    I simplify my problem like below
    >
    To convert list
    aa = [0x12, 0x34, 0x56, 0x78]
    into
    [0x34, 0x12, 0x78, 0x56]
    >
    How to do it fast? My real list is huge.
    >
    Thanks a lot.
    Jason

    Comment

    • Simon Forman

      #3
      Re: swapping numeric items in a list

      Jiang Nutao wrote:
      Hi,
      >
      I simplify my problem like below
      >
      To convert list
      aa = [0x12, 0x34, 0x56, 0x78]
      into
      [0x34, 0x12, 0x78, 0x56]
      >
      How to do it fast? My real list is huge.
      >
      Thanks a lot.
      Jason
      Here's simple and probably fast enough way (but it won't work right on
      odd length lists):

      def rev(n):
      i = iter(n)
      while True:
      a = i.next()
      yield i.next()
      yield a


      Example of use:

      r = range(24)

      print list(rev(r))

      If your list comes from binary (str) data, and you're dealing with
      endianness, I smell a faster way using struct.

      Peace,
      ~Simon

      Comment

      • bearophileHUGS@lycos.com

        #4
        Re: swapping numeric items in a list

        Jiang Nutao:
        To convert list
        aa = [0x12, 0x34, 0x56, 0x78]
        into
        [0x34, 0x12, 0x78, 0x56]
        How to do it fast? My real list is huge.
        Note that:
        >>a = range(6)
        >>a
        [0, 1, 2, 3, 4, 5]
        >>a[::2]
        [0, 2, 4]
        >>a[1::2]
        [1, 3, 5]

        So you can do:
        >>a[::2], a[1::2] = a[1::2], a[::2]
        >>a
        [1, 0, 3, 2, 5, 4]

        Bye,
        bearophile

        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: swapping numeric items in a list

          In <mailman.9675.1 156291509.27775 .python-list@python.org >, Jiang Nutao
          wrote:
          To convert list
          aa = [0x12, 0x34, 0x56, 0x78]
          into
          [0x34, 0x12, 0x78, 0x56]
          >
          How to do it fast? My real list is huge.
          Use the `array` module and the `array.byteswap ()` method.

          Ciao,
          Marc 'BlackJack' Rintsch

          Comment

          • Boris Borcic

            #6
            Re: swapping numeric items in a list

            Jiang Nutao wrote:
            Hi,
            >
            I simplify my problem like below
            >
            To convert list
            aa = [0x12, 0x34, 0x56, 0x78]
            into
            [0x34, 0x12, 0x78, 0x56]
            >
            How to do it fast? My real list is huge.
            Mark Rintsch's suggestion appears best if applicable, but just to cite yet other
            ways to do it :

            list(aa[j+(-1)**j] for j in range(len(aa)))

            or

            sum(reversed(zi p(*[reversed(aa)]*2)),())

            or (py 2.5)

            from itertools import izip
            ab = iter(aa)
            list((yield y) or x for x,y in izip(ab,ab))

            Comment

            • Jiang Nutao

              #7
              Re: swapping numeric items in a list

              Thank you all guys for the help. Guess I'm gonna pick bearophile's way. It's
              fast, neat, and easy to read.

              array.byteswap( ) won't work for me easily. I tried this before my 1st post.
              I defined

              aa = array('H', [0x12, 0x34, 0x56, 0x78])

              Then did byteswap aa.byteswap(). The result was

              array('H', [0x1200, 0x3400, 0x5600, 0x7800])

              You can see it byteswapped within each item.

              Jason

              "Marc 'BlackJack' Rintsch" <bj_666@gmx.net wrote in message
              news:pan.2006.0 8.23.10.13.15.8 54509@gmx.net.. .
              In <mailman.9675.1 156291509.27775 .python-list@python.org >, Jiang Nutao
              wrote:
              >
              >To convert list
              > aa = [0x12, 0x34, 0x56, 0x78]
              >into
              > [0x34, 0x12, 0x78, 0x56]
              >>
              >How to do it fast? My real list is huge.
              >
              Use the `array` module and the `array.byteswap ()` method.
              >
              Ciao,
              Marc 'BlackJack' Rintsch
              --

              >


              Comment

              • Gabriel Genellina

                #8
                Re: swapping numeric items in a list

                At Wednesday 23/8/2006 14:44, Jiang Nutao wrote:
                >array.byteswap () won't work for me easily. I tried this before my 1st post.
                >I defined
                >
                aa = array('H', [0x12, 0x34, 0x56, 0x78])
                >
                >Then did byteswap aa.byteswap(). The result was
                >
                array('H', [0x1200, 0x3400, 0x5600, 0x7800])
                >
                >You can see it byteswapped within each item.
                Use array('b') or 'B'. 'H' are two-byes integers.



                Gabriel Genellina
                Softlab SRL





                _______________ _______________ _______________ _____
                Preguntá. Respondé. Descubrí.
                Todo lo que querías saber, y lo que ni imaginabas,
                está en Yahoo! Respuestas (Beta).
                ¡Probalo ya!


                Comment

                • Jiang Nutao

                  #9
                  Re: swapping numeric items in a list

                  This is what I got in the debugger:

                  (Pdb) aa=array('b', [126, 55, 71, 112])
                  (Pdb) aa
                  array('b', [126, 55, 71, 112])
                  (Pdb) aa.byteswap()
                  (Pdb) aa
                  array('b', [126, 55, 71, 112])

                  ----- Original Message -----
                  From: "Gabriel Genellina" <gagsl-py@yahoo.com.ar >
                  To: "Jiang Nutao" <jiangnutao@gma il.com>
                  Cc: <python-list@python.org >
                  Sent: Wednesday, August 23, 2006 11:28 AM
                  Subject: Re: swapping numeric items in a list

                  At Wednesday 23/8/2006 14:44, Jiang Nutao wrote:
                  >
                  >>array.byteswa p() won't work for me easily. I tried this before my 1st
                  >>post.
                  >>I defined
                  >>
                  > aa = array('H', [0x12, 0x34, 0x56, 0x78])
                  >>
                  >>Then did byteswap aa.byteswap(). The result was
                  >>
                  > array('H', [0x1200, 0x3400, 0x5600, 0x7800])
                  >>
                  >>You can see it byteswapped within each item.
                  >
                  Use array('b') or 'B'. 'H' are two-byes integers.
                  >
                  >
                  >
                  Gabriel Genellina
                  Softlab SRL
                  >
                  >
                  >
                  >
                  _______________ _______________ _______________ _____
                  Preguntá. Respondé. Descubrí.
                  Todo lo que querías saber, y lo que ni imaginabas,
                  está en Yahoo! Respuestas (Beta).
                  ¡Probalo ya! http://www.yahoo.com.ar/respuestas
                  >
                  >

                  Comment

                  • Fredrik Lundh

                    #10
                    Re: swapping numeric items in a list

                    Jiang Nutao wrote:
                    array.byteswap( ) won't work for me easily. I tried this before my 1st post.
                    I defined
                    >
                    aa = array('H', [0x12, 0x34, 0x56, 0x78])
                    >
                    Then did byteswap aa.byteswap(). The result was
                    >
                    array('H', [0x1200, 0x3400, 0x5600, 0x7800])
                    >
                    You can see it byteswapped within each item.
                    you need to do things in the right order; first convert to bytes, then
                    build a 16-bit array, and then byteswap that array.

                    from array import array

                    # convert data array to 8-bit byte buffer
                    buf = array("B", [0x12, 0x34, 0x56, 0x78]).tostring()

                    # treat byte buffer as list of 16-bit integers
                    buf = array("H", buf)
                    buf.byteswap()

                    buf = buf.tostring() # convert back to bytes

                    [hex(ord(c)) for c in buf]
                    -['0x34', '0x12', '0x78', '0x56']

                    </F>

                    Comment

                    • Gabriel Genellina

                      #11
                      Re: swapping numeric items in a list

                      At Wednesday 23/8/2006 15:32, Jiang Nutao wrote:
                      >This is what I got in the debugger:
                      >
                      >(Pdb) aa=array('b', [126, 55, 71, 112])
                      >(Pdb) aa
                      >array('b', [126, 55, 71, 112])
                      >(Pdb) aa.byteswap()
                      >(Pdb) aa
                      >array('b', [126, 55, 71, 112])
                      Oh, sorry, to swap by two bytes "H" was the right typecode. But your
                      input array should be:
                      >> aa = array('H', [0x1234, 0x5678])
                      which should give:
                      array('H', [0x3412, 0x7856])



                      Gabriel Genellina
                      Softlab SRL





                      _______________ _______________ _______________ _____
                      Preguntá. Respondé. Descubrí.
                      Todo lo que querías saber, y lo que ni imaginabas,
                      está en Yahoo! Respuestas (Beta).
                      ¡Probalo ya!


                      Comment

                      • Jiang Nutao

                        #12
                        Re: swapping numeric items in a list

                        Thanks. But the thing I need to swap is [0x12, 0x34, 0x56, 0x78], not
                        [0x1234, 0x5678].

                        ----- Original Message -----
                        From: "Gabriel Genellina" <gagsl-py@yahoo.com.ar >
                        To: "Jiang Nutao" <jiangnutao@gma il.com>
                        Cc: "Gabriel Genellina" <gagsl-py@yahoo.com.ar >; <python-list@python.org >
                        Sent: Wednesday, August 23, 2006 12:19 PM
                        Subject: Re: swapping numeric items in a list

                        At Wednesday 23/8/2006 15:32, Jiang Nutao wrote:
                        >
                        >>This is what I got in the debugger:
                        >>
                        >>(Pdb) aa=array('b', [126, 55, 71, 112])
                        >>(Pdb) aa
                        >>array('b', [126, 55, 71, 112])
                        >>(Pdb) aa.byteswap()
                        >>(Pdb) aa
                        >>array('b', [126, 55, 71, 112])
                        >
                        Oh, sorry, to swap by two bytes "H" was the right typecode. But your input
                        array should be:
                        >>> aa = array('H', [0x1234, 0x5678])
                        which should give:
                        array('H', [0x3412, 0x7856])
                        >
                        >
                        >
                        Gabriel Genellina
                        Softlab SRL
                        >
                        >
                        >
                        >
                        _______________ _______________ _______________ _____
                        Preguntá. Respondé. Descubrí.
                        Todo lo que querías saber, y lo que ni imaginabas,
                        está en Yahoo! Respuestas (Beta).
                        ¡Probalo ya! http://www.yahoo.com.ar/respuestas
                        >
                        >

                        Comment

                        Working...