0
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,9])

How to add 2 to arr[0:2] and arr[5:6] so the final result is:

arr[3,4,3,4,5,8,7,8,9]
1
  • arr[np.r_[range(2), 5, 6]] += 2 Commented May 20, 2022 at 18:55

2 Answers 2

2

Note that you could create an array that has all the indices to the values that need to be modified:

arr[np.r_[0:2, 5:6]] += 2
print(arr)

Out:

array([3, 4, 3, 4, 5, 8, 7, 8, 9])
Sign up to request clarification or add additional context in comments.

Comments

1

Very straight forward :)

import numpy as np

arr = np.array([1,2,3,4,5,6,7,8,9])

arr[0:2] += 2
arr[5:6] += 2

print(arr)

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.