0

To the following array

my_array = np.array([[11,12],[21,22],[31,32]])

I want to add 100 to the even values, so I write:

my_array[my_array % 2==0]+=100
print(my_array)
[[ 11 112]
 [ 21 122]
 [ 31 132]]

which is fine. Now if I write it with the plus symbol on the other side I get:

my_array[my_array % 2==0]=+100
print(my_array)
[[ 11 100]
 [ 21 100]
 [ 31 100]]

It seems to replace instead of adding the value, or to add the value to the result of the filter. Could somebody explain to me the reasons behind this and if this is the expected behaviour? Thanks !!!

2
  • Second step is simply assigning +100 i.e. 100 to those masked places. What exactly is the confusion again? Commented Nov 1, 2017 at 8:45
  • 1
    I think OP is confused. a+=b is same as a = a+b. a =+ b is nothing but a = (+b) Commented Nov 1, 2017 at 9:07

1 Answer 1

2

There is a difference in += and =+

x += 1 is the same as x = x + 1

but x =+1 is just saying x = +1, i.e you are assigning x to the value of positive one.

So it makes sense that in your second case you are assigning the value 100 instead of adding it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.