简体   繁体   English

如何用给定范围的值部分填充numpy数组?

[英]How can I partially fill a numpy array with a value given a range?

Given a numpy array, a range, and a value, how can I fill the array with the value inside the range? 给定一个numpy数组,一个范围和一个值,如何用范围内的值填充数组?

Is there a way that is faster than filling it in manually with a loop one by one? 有没有比手动逐个循环填充更快的方法?

Edit: 编辑:

myArray = np.zeros(10)

What I want: [0, 0, 0, 0, 1, 1, 1, 0, 0, 0] 我想要的是:[0,0,0,0,1,1,1,0,0,0]

arr = np.zeros(10)
arr[4:7] = 1
print(arr)

Output: 输出:

array([0., 0., 0., 0., 1., 1., 1., 0., 0., 0.])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何过滤给定的 numpy 数组以获取值并将数组的剩余值放入新数组中? - How can i filter a given numpy array for a value and put the remaining values of the array into a new one? 如何使一个新的numpy数组与给定的数组大小相同,并用标量值填充它 - how to make a new numpy array same size as a given array and fill it with a scalar value 如何用空的内置列表填充 numpy 数组? - How can I fill a numpy array with empty builtin lists? 我如何从函数填充一个numpy结构化数组? - How can I fill a numpy structured array from a function? 如何沿着 numpy 数组的给定轴索引每个出现的最大值? - How can I index each occurrence of a max value along a given axis of a numpy array? 如何找到 NumPy 数组中等于给定值的元素? - How do I locate an element in a NumPy array equal to a given value? 如何初始化具有给定维数的空 Numpy 数组? - How can I initialize an empty Numpy array with a given number of dimensions? 如果我们在给定范围之外的numpy数组中分配一个值,如何引发异常? - How to raise an exception if we assign a value in a numpy array outside of a given range? 如何用给定索引/坐标的零填充numpy数组的零 - How to fill numpy array of zeros with ones given indices/coordinates 如何用给定索引范围/切片的零填充 numpy 数组 - How to fill numpy array of zeros with ones given index ranges/slices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM