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

Nico nicolist at limare.net
Fri Feb 10 12:25:04 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 forst 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]

which is better, in terms of speed (and memory management), for large 3D


More information about the Numpy-discussion mailing list