1

Image

I am trying to create for loop for submatrix, let say

matrix = [
    [1, 0, 1, 3],
    [0, 2, 2, 2],
    [1, 2, 3, 4],
    [1, 2, 3, 4]
]

I want to print submatrix from matrix[2][3] to matrix[4][4] where index start at 1.

Output what i expect -

    [2, 2]
    [3, 4]
    [3, 4]

2 Answers2

0

You could use a range for those coordinates, just keep in mind that list indexes in python are zero based and that ranges are inclusive of the start index but exclusive of the end index:

for row in range(2, 5):
    for col in range (3, 5):
       print(matrix[row - 1][col - 1], end=' ')
    print()
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

This could work

print([x[2:] for x in matrix[1:]])

salilnaik
  • 46
  • 5