Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Tuesday, January 27, 2009

Snippets: Creating a matrix in python

There's two easy ways to do this:

Looping:

def matrix(rownumber,columnumber):
matrix = []
for i in range(rownumber):
a = [0]*columnumber
matrix.append(a)
return matrix


List comprehensions:

def matrix(rownumber, columnumber):
return [[0]*columnumber for x in range(rownumber)]