Boa v0.1.0-alpha + unrelated question

Jeff Shannon jeff at ccvcorp.com
Fri Mar 29 13:55:42 EST 2002


In article <a81r2k$8qr$1 at newsg2.svr.pol.co.uk>, 
buzzard at urubu.freeserve.co.uk says...

> On an unrelated note; is there a better (more Pythonic / more efficient) way
> of swapping two rows in a Numeric array than the following.
> 
> def swapRows(array, index1, index2):
>     temp = array[index2, :].astype(type(array))
>     array[index2, :] = array[index1, :]
>     array[index1, :] = temp

Well, I don't know about Numeric specifically, but the 
'canonical' Python swap idiom would make it this:

def SwapRows(array, i1, i2):
    array[i1, :], array[i2, :] = array[i2, :], array[i1, :]

(In general, 'a, b = b, a' creates a temporary tuple, then 
unpacks it back to the original names but in reverse order.  
However, I can't swear that this will work with Numeric because 
I've never used Numeric, and it may be doing odd things that 
don't interact well with being packed into a temporary tuple.)

-- 

Jeff Shannon
Technician/Programmer
Credit International



More information about the Python-list mailing list