Skip to content

Instantly share code, notes, and snippets.

@waynemoore
Created July 26, 2011 09:35
Show Gist options
  • Save waynemoore/1106378 to your computer and use it in GitHub Desktop.
Save waynemoore/1106378 to your computer and use it in GitHub Desktop.
Transpose a matrix in Python
def transpose(matrix):
num_rows = len(matrix)
num_cols = len(matrix[0])
t_matrix = [[None for _ in range(num_rows)] for _ in range(num_cols)]
for y in range(num_rows):
for x in range(num_cols):
t_matrix[x][y] = matrix[y][x]
return t_matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment