[Numpy-discussion] swap elements in two arrays
Alan G Isaac
Sun Feb 7 09:39:12 CST 2010
On 2/7/2010 10:21 AM, Alan Isaac wrote:
>> I have two 1d arrays, say `a` and `b`.
>> I need to swap elements if a 1d boolean criterion `to_swap` is met.
>> Here is a much faster way:
>> a[to_swap], b[to_swap] = b[to_swap], a[to_swap]
On 2/7/2010 10:21 AM, Pauli Virtanen wrote:
> 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`...
I thought that if `to_swap` is a boolean array that `a[to_swap]`
will always own its own data. Can that fail?
More information about the NumPy-Discussion