如何更改numpy数组中的数据块

Uwa*_*s A 1 python numpy

numpy在Python中有一个大的1维数据数组,并希望条目x(500)到y(520)更改为等于1.我可以使用for循环,但有更简洁,更快的numpy方法吗?

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

这是可以使用的for循环,但似乎我可能会在numpy中找到一个函数 - 我宁愿不使用numpy提供的掩码数组

Rub*_*dez 5

您可以使用[]访问一系列元素:

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.]
Run Code Online (Sandbox Code Playgroud)