Python - List Comprehensions and Matrixes

Introduction

List comprehensions can process Matrixes.

Demo

M = [[1, 2, 3], 
     [4, 5, 6], #   ww w  .java2 s.  c om
     [7, 8, 9]] 


print( M[1] )              # Row 2 
print( M[1][2] )           # Row 2, item 3 

d=[row[1] for row in M]                          # Column 2 
print( d )

d=[M[row][1] for row in (0, 1, 2)]               # Using offsets 
print( d )

Result

Related Topic