[Numpy-discussion] swap elements in two arrays

Pauli Virtanen pav@iki...
Sun Feb 7 09:21:41 CST 2010


su, 2010-02-07 kello 10:11 -0500, Alan G Isaac kirjoitti:
> I have two 1d arrays, say `a` and `b`.
> I need to swap elements if a 1d boolean criterion `to_swap` is met.
[clip]
> Here is a much faster way:
>          a[to_swap], b[to_swap] = b[to_swap], a[to_swap]

That doesn't necessarily work -- the above code expands to

	tmp = a[to_swap]
	a[to_swap] = b[to_swap]
	b[to_swap] = tmp

It'll work provided `to_swap` is such that `tmp` is not a view on `a`...

-- 
Pauli Virtanen




More information about the NumPy-Discussion mailing list