[Numpy-discussion] Assignment function with a signature similar to take?

Eric Moore ewm at redtetrahedron.org
Sat Nov 17 09:25:48 EST 2012


Is there a function that operates like 'take' but does assignment? 
Specifically that takes indices and an axis?  As far as I can tell no 
such function exists.  Is there any particular reason?

One can fake such a thing by doing (code untested):

s = len(a.shape)*[np.s_[:]]
s[axis] = np.s_[1::2]
a[s] = b.take(np.arange(1,b.shape[axis],2), axis)

Or by using np.rollaxis:

a = np.rollaxis(a, axis, len(a.shape))
a[..., 1::2] = b[..., 1::2]
a = np.rollaxis(a, len(a.shape)-1, axis)

But I don't really think that either of these are particularly clear, 
but probably prefer the rollaxis solution.

Also, while I'm here, what about having take also be able to use a slice 
object in lieu of a collection of indices?

-Eric



More information about the NumPy-Discussion mailing list