[英]How to fill numpy array by row and column
I want to fill the first and the last row and the first and the last column of a grid with 4
. 我想,以填补第一和最后一行与第一和网格与最后一列
4
。
Input: 输入:
grid = np.zeros((5, 5))
[[ 0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. ]
[ 0. 0. 0. 0. 0. ]]
output: 输出:
[[ 4. 4. 4. 4. 4. ]
[ 4. 0. 0. 0. 4. ]
[ 4. 0. 0. 0. 4. ]
[ 4. 0. 0. 0. 4. ]
[ 4. 4. 4. 4. 4. ]]
I tried to do this with a for loop but is there a better option than that? 我试图通过for循环来做到这一点,但是有没有比这更好的选择了?
To change the values of first and last rows: 要更改第一行和最后一行的值:
grid[[0,-1],:] = 4
To change the values of first and last columns: 要更改第一列和最后一列的值:
grid[:, [0,-1]] = 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.