def shape(a): m = len(a) n = len(a[0]) return (m, n) def zeros(m, n): row = [] #A row of the matrix for i in range(0, n): #This adds the correct number of 0s to 'row' row.append(0) z = [] #The matrix for i in range (0, m): #This adds the correct number of 'row's to 'z' z.append(list(row)) return z #returns the matrix def matrix_transpose(a): m,n = shape(a) b = zeros(n, m) print b for i in range(n): for j in range(m): b[i][j] = a[j][i] return b print matrix_transpose([[9,2,2], [4,5,6]])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question