473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Aug 22 '06 #1
11 5455
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
Aug 23 '06 #2
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

Aug 23 '06 #3
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

Aug 23 '06 #4
In <ma***************************************@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
Aug 23 '06 #5
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(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))

Aug 23 '06 #6
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****@gmx.netwrote in message
news:pa****************************@gmx.net...
In <ma***************************************@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
--
http://mail.python.org/mailman/listinfo/python-list


Aug 23 '06 #7
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

Aug 23 '06 #8
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" <ga******@yahoo.com.ar>
To: "Jiang Nutao" <ji********@gmail.com>
Cc: <py*********@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.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

Aug 23 '06 #9
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>

Aug 23 '06 #10
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

Aug 23 '06 #11
Thanks. But the thing I need to swap is [0x12, 0x34, 0x56, 0x78], not
[0x1234, 0x5678].

----- Original Message -----
From: "Gabriel Genellina" <ga******@yahoo.com.ar>
To: "Jiang Nutao" <ji********@gmail.com>
Cc: "Gabriel Genellina" <ga******@yahoo.com.ar>; <py*********@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

Aug 23 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Johannes Nix |Johannes.Nix | last post by:
Hi, I have a tricky problem with Numeric. Some time ago, I have generated a huge and complex data structure, and stored it using the cPickle module. Now I want to evaluate it quickly again on a...
2
by: benjamin_r | last post by:
I am trying to use the std::accumulate function. When I compile the following code I get: error C2780: '_Ty std::accumulate(_InIt,_InIt,_Ty,_Fn2)' : expects 4 arguments - 3 provided. It should...
2
by: Sam Marrocco | last post by:
Does anyone know of a method of swapping collection items? For example: Current collection: ..Item(1)="a" ..Item(2)="b" ..Item(3)="c" After swapping items 2 and 1.... ..Item(1)="b"...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
6
by: Jon Slaughter | last post by:
I would like to use the numericUpDown in it standard way but have to features. I would like to display items instead of numbers that amp to the numbers and have the control be "cyclic" in the sense...
14
by: Ben | last post by:
I have recently learned how list comprehension works and am finding it extremely cool. I am worried, however, that I may be stuffing it into places that it does not belong. What's the most...
2
by: =?Utf-8?B?RHJEQkY=?= | last post by:
I understand that the Value put into a DataGridViewComboBoxCell has to be a member of the Items list or an exception is thrown. I also understand that you can override that exception by handling...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.