convert a matrix(list of lists) into a square matrix in python -
i want convert given matrices a, b square matrices inserting zeros wherever necessary
a = [[1,2],[3,4],[5,6],[7,8]] b = [[1,2,3,4],[5,6,7,8]]
i want output
a1 = [[1,2,0,0],[3,4,0,0],[5,6,0,0],[7,8,0,0]] b1 = [[1,2,3,4],[5,6,7,8][0,0,0,0],[0,0,0,0]]
i have problem installing numpy bundle on machine. solution without utilize of numpy help.
thanks
>>> = [[1,2], [3,4], [5,6], [7,8]] >>> b = [[1,2,3,4], [5,6,7,8]] >>> >>> def matrix(a, n): ... row in a: ... yield row + [0] * (n - len(row)) ... in range(len(a), n): ... yield [0] * n ... >>> list(matrix(a, 4)) [[1, 2, 0, 0], [3, 4, 0, 0], [5, 6, 0, 0], [7, 8, 0, 0]] >>> list(matrix(b, 4)) [[1, 2, 3, 4], [5, 6, 7, 8], [0, 0, 0, 0], [0, 0, 0, 0]]
python list matrix
No comments:
Post a Comment