def give_matrix(i,j): res = [] for ii in range(i-2,i+3): inner_res = [] for jj in range(j-2,j+3): if (ii-2<0 or ii+3>n or jj-2<0 or jj+3>n): inner_res.append('x') else: inner_res.append(arr[ii][jj]) res.append(inner_res) return res m = 20 n = 20 arr = [[i+j for i in range(m)] for j in range(n)] print(give_matrix(1,10)) print(give_matrix(8,12))
Standard input is empty
[['x', 'x', 'x', 'x', 'x'], ['x', 'x', 'x', 'x', 'x'], ['x', 'x', 'x', 'x', 'x'], [10, 11, 12, 13, 14], [11, 12, 13, 14, 15]] [[16, 17, 18, 19, 20], [17, 18, 19, 20, 21], [18, 19, 20, 21, 22], [19, 20, 21, 22, 23], [20, 21, 22, 23, 24]]