1

I have a large numpy 1 dimensional array of data in Python and want entries x (500) to y (520) to be changed to equal 1. I could use a for loop but is there a neater, faster numpy way of doing this?

for x in range(500,520) numpyArray[x] = 1.

Here is the for loop that could be used but it seems like there could be a function in numpy that I'm missing - I'd rather not use the masked arrays that numpy offers

0

1 Answer 1

5

You can use [] to access a range of elements:

import numpy as np

a = np.ones((10))
print(a) # Original array
# [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
startindex = 2
endindex = 4
a[startindex:endindex] = 0

print(a) # modified array
# [ 1.  1.  0.  0.  1.  1.  1.  1.  1.  1.]
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.