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 -- http://mail.python.org/mailman/listinfo/python-list
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" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > In <[EMAIL PROTECTED]>, 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 > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
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" <[EMAIL PROTECTED]> To: "Jiang Nutao" <[EMAIL PROTECTED]> Cc: 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.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! http://www.yahoo.com.ar/respuestas > > -- http://mail.python.org/mailman/listinfo/python-list
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" <[EMAIL PROTECTED]> To: "Jiang Nutao" <[EMAIL PROTECTED]> Cc: "Gabriel Genellina" <[EMAIL PROTECTED]>; 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 > > -- http://mail.python.org/mailman/listinfo/python-list