Sub matrix of a list of lists (without numpy)

前端 未结 4 1372
误落风尘
误落风尘 2020-12-09 12:04

Suppose I have a matrix composed of a list of lists like so:

>>> LoL=[list(range(10)) for i in range(10)]
>>> LoL
[[0, 1, 2, 3, 4, 5, 6, 7,         


        
4条回答
  •  执念已碎
    2020-12-09 12:44

    Do this,

    submat = [ [ mat[ i ][ j ] for j in range( index1, index2 ) ] for i in range( index3, index4 ) ]

    the submat will be the rectangular (square if index3 == index1 and index2 == index4) chunk of your original big matrix.

提交回复
热议问题