numarray iterator question
This is a bit confusing to me: import numpy as np u = np.ones ((3,3)) for u_row in u: u_row = u_row * 2 << doesn't work print u [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]] for u_row in u: u_row *= 2 << does work [[ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.]] Naively, I'm thinking a *= b === a = a * b. Is this behavior expected? I'm asking because I really want: u_row = my_func (u_row)
On Wed, Mar 10, 2010 at 18:19, Neal Becker <ndbecker2@gmail.com> wrote:
This is a bit confusing to me:
import numpy as np
u = np.ones ((3,3))
for u_row in u: u_row = u_row * 2 << doesn't work
print u [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]]
for u_row in u: u_row *= 2 << does work [[ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.]]
Naively, I'm thinking a *= b === a = a * b.
http://docs.python.org/reference/simple_stmts.html#augmented-assignment-stat... """An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead."""
Is this behavior expected? I'm asking because I really want:
u_row = my_func (u_row)
Iterate over indices instead: for i in range(len(u)): u[i] = my_func(u[i]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Neal Becker wrote:
This is a bit confusing to me:
import numpy as np
u = np.ones ((3,3))
for u_row in u: u_row = u_row * 2 << doesn't work
Try this instead: for u_row in u: u_row[:] = u_row * 2 Warren
print u [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]]
for u_row in u: u_row *= 2 << does work [[ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.]]
Naively, I'm thinking a *= b === a = a * b.
Is this behavior expected? I'm asking because I really want:
u_row = my_func (u_row)
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Neal Becker -
Robert Kern -
Warren Weckesser