fork download
  1. def give_matrix(i,j):
  2. res = []
  3. for ii in range(i-2,i+3):
  4. inner_res = []
  5. for jj in range(j-2,j+3):
  6. if (ii-2<0 or ii+3>n or jj-2<0 or jj+3>n):
  7. inner_res.append('x')
  8. else:
  9. inner_res.append(arr[ii][jj])
  10. res.append(inner_res)
  11. return res
  12.  
  13.  
  14. m = 20
  15. n = 20
  16.  
  17. arr = [[i+j for i in range(m)] for j in range(n)]
  18.  
  19. print(give_matrix(1,10))
  20. print(give_matrix(8,12))
  21.  
Success #stdin #stdout 0.02s 9936KB
stdin
Standard input is empty
stdout
[['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]]