[SciPy-User] numpy array assignment
Antonino Ingargiola
tritemio at gmail.com
Thu Apr 17 18:12:23 EDT 2014
On Thu, Apr 17, 2014 at 7:59 AM, marco cammarata <marcocamma at gmail.com>wrote:
> For the moment I ended up doing something really really inelegant:
> res = result_of_long_calculation_returning_the_right_shape()
> s = "out[" + (":,")*axis + "i" + (",:")*(nDim-axis-1) + "]=res"
> exec(s)
>
When you do a[:, :, :], that's just syntactic sugar for a[(slice(None),
slice(None), slice(None),)] (note that slice(None) means take all
elements). In other words, the index of a ndarray is a tuple of
objects/slices.
You can build the tuple programmatically. Another example:
import numpy as np
a = np.arange(1000).reshape(10, 5, 20)
index = (slice(None), slice(0, 1), slice(None))
a[index] = 3
a
Cheers,
Antonio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20140417/38eb96f4/attachment.html>
More information about the SciPy-User
mailing list