[Numpy-discussion] write access through iterator

Mark Wiebe mwwiebe@gmail....
Wed Jun 22 13:39:48 CDT 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@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@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110622/d3a8490f/attachment-0001.html 


More information about the NumPy-Discussion mailing list