Python Numpy: Correcting Syntax Error in Array Slicing
The code you provided has a syntax error. The correct syntax for slicing in Python requires a comma between the start and end indices for each dimension. Here's the corrected code:
import numpy as np
Z = np.zeros((5,5))
Z[1:-1, 1:-1] = 1
Z
This code creates a 5x5 array of zeros and then sets all elements except for the outermost row and column to 1. The resulting array Z looks like this:
array([[0., 0., 0., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 1., 1., 1., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 0., 0., 0.]])
原文地址: http://www.cveoy.top/t/topic/bi5k 著作权归作者所有。请勿转载和采集!