Re: swapping numeric items in a list

2006-08-23 Thread Jiang Nutao
>; 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(&#x

Re: swapping numeric items in a list

2006-08-23 Thread Gabriel Genellina
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

Re: swapping numeric items in a list

2006-08-23 Thread Fredrik Lundh
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 byteswappe

Re: swapping numeric items in a list

2006-08-23 Thread Jiang Nutao
]> 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 >&g

Re: swapping numeric items in a list

2006-08-23 Thread Gabriel Genellina
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

Re: swapping numeric items in a list

2006-08-23 Thread Jiang Nutao
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 ar

Re: swapping numeric items in a list

2006-08-23 Thread Boris Borcic
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

Re: swapping numeric items in a list

2006-08-23 Thread Marc 'BlackJack' Rintsch
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.py

Re: swapping numeric items in a list

2006-08-22 Thread bearophileHUGS
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

Re: swapping numeric items in a list

2006-08-22 Thread Simon Forman
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

Re: swapping numeric items in a list

2006-08-22 Thread faulkner
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 li