Trying to transpose the matrix from a 3x4 to a 4x3 in python -


i have read file command line, , set matrix rows , cols. when run program(below):

python3 transpose.py               matrix                                                 3 4         (current code below) (file numbers 1 2 3 4 5 6 7 8 9 10 11 12 13)  (matrix row,cols) 

printed should return:

the empty matrix is:[[0,0,0,0],[0,0,0,0],[0,0,0,0]] original matrix is: 1 2 3 4 5 6 7 8 9 10 11 12  transposed matrix is: 1 5 9 2 6 10 3 7 11 4 8 12 

here transpose.py ive spent around 10 hours on , can't work, can't figure out else do!

 import sys scanner import *   def main():     readinput(sys.argv[1],[2])     size = 3     rows = size     cols = rows     makelist(size)     matrix(rows,cols)     manipulatematrix(matrix(rows,cols))     print(manipulatematrix(matrix(rows,cols)))     print("the original matrix is: ")     displaymatrix(manipulatematrix(matrix(rows,cols)))     transposesquare(manipulatematrix(matrix(rows,cols)),size)     print (transposesquare(manipulatematrix(matrix(rows,cols)),size))     print("the transposed matrix: ")     displaymatrix(transposesquare(manipulatematrix(matrix(rows,cols)),size))     def readinput(filename,matrix):     s = scanner(filename)     r = s.readtoken()     while r != "":         r = int(r)         c = s.readint()         v = s.readint()         matrix[r][c]=v         r = s.readtoken()     s.close()  def makelist(size):     lyst = []     in range(size):         lyst = lyst + [none]     return lyst  def matrix(rows,cols):     matrix = makelist(rows)     in range(rows):         matrix[i] = makelist(cols)         return matrix  def manipulatematrix(m):     rows = len(m)     cols = len(m[0])     count = 1     r in range(0,rows,1):         c in range(0,cols,1):             m[r][c] = count             count += 1     return m  def transposesquare(m,size):     r in range(0,size):          c in range(0,size):             m[r][c],m[c][r] = m[c][r],m[r][c]     return m  def displaymatrix(m):     m == rows == cols     r in range(0,size):         c in range(0,cols,1):             print(m[r][c],end = "")         print()     return           main() 

right stuck @

 traceback (most recent call last):   file "transpose.py", line 66, in <module>     main()   file "transpose.py", line 6, in main     readinput(sys.argv[1],[2])   file "transpose.py", line 27, in readinput     matrix[r][c]=v indexerror: list index out of range 

transposing 2d list simpler. use zip() function * unpacking operator:

>>> matrix = [[1,2,3,4], [5,6,7,8], [9,10,11,12]] >>> new = list(zip(*matrix))  # call list not necessary in python 2 >>> new [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)] 

or, if don't want tuples:

>>> new = [list(tup) tup in zip(*matrix)] >>> new [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]] 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -