[Numpy-discussion] array shift and |=, copy, and backward operations

Nico nicolist at limare.net
Fri Feb 10 12:47:02 CST 2006


> a = array([0,1,0,0])
> a[1:] |= a[:-1]
>
> gives the unexpected result [0 1 1 1] instead of [0 1 1 0] because
> python performs the |= on the first cell, then on the second, and so on.
>
> I found two ways to get it right, with a copy:
> b = a.copy()
> a[1:] |= b[:-1]
>
> or working backward:
> a[-1:1:-1] |= a[-2:0:-1]

I finally noticed that

a = array([0,1,0,0])
a[1:] |= a[:-1] | False

also works, but I can't figure out why...

-- 
Nico




More information about the Numpy-discussion mailing list