[Numpy-discussion] write access through iterator

Mark Wiebe mwwiebe at gmail.com
Wed Jun 22 14:39:48 EDT 2011


You can do something pretty close to that with the 1.6 iterator:

>>> u = np.arange(20).reshape(5,4)[:,:3]
>>> u
array([[ 0,  1,  2],
       [ 4,  5,  6],
       [ 8,  9, 10],
       [12, 13, 14],
       [16, 17, 18]])
>>> for item in np.nditer(u, [], ['readwrite'], order='C'):
...     item[...] = 10
...
>>> u
array([[10, 10, 10],
       [10, 10, 10],
       [10, 10, 10],
       [10, 10, 10],
       [10, 10, 10]])

-Mark


On Wed, Jun 22, 2011 at 1:33 PM, Neal Becker <ndbecker2 at gmail.com> wrote:

> Maybe I'm being dense today, but I don't see how to iterate over arrays
> with
> write access.  You could read through iterators like:
>
> fl = u.flat
> >>> for item in fl:
> ...     print item
>
> but you can't do
>
> for item in fl:
>  item = 10
>
> (or, it won't do what you want).
>
> Is there any way to do this?
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110622/d3a8490f/attachment.html>


More information about the NumPy-Discussion mailing list