swapping numeric items in a list
Simon Forman
rogue_pedro at yahoo.com
Tue Aug 22 20:50:47 EDT 2006
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
More information about the Python-list
mailing list