swapping numeric items in a list

J

Jiang Nutao

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
 
F

faulkner

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

Simon Forman

Jiang said:
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
 
M

Marc 'BlackJack' Rintsch

Jiang Nutao said:
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
 
B

Boris Borcic

Jiang said:
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(zip(*[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))
 
J

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

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

You can see it byteswapped within each item.

Jason

Marc 'BlackJack' Rintsch said:
Jiang Nutao said:
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
 
G

Gabriel Genellina

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
 
J

Jiang Nutao

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: <[email protected]>
Sent: Wednesday, August 23, 2006 11:28 AM
Subject: Re: swapping numeric items in a list

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
 
F

Fredrik Lundh

Jiang said:
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>
 
G

Gabriel Genellina

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
 
J

Jiang Nutao

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]>; <[email protected]>
Sent: Wednesday, August 23, 2006 12:19 PM
Subject: 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])

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top