I have an array of a size 2 x 2 and I want to change the size to 3 x 4.
A = [[1 2 ],[2 3]]
A_new = [[1 2 0 0],[2 3 0 0],[0 0 0 0]]
I tried
Pure Python way achieve this:
row = 3
column = 4
A = [[1, 2],[2, 3]]
A_new = map(lambda x: x + ([0] * (column - len(x))), A + ([[0] * column] * (row - len(A))))
then A_new
is [[1, 2, 0, 0], [2, 3, 0, 0], [0, 0, 0, 0]]
.
Good to know:
[x] * n
will repeat x
n
-times+
operatorExplanation:
map(function, list)
will iterate each item in list pass it to function and replace that item with the return valueA + ([[0] * column] * (row - len(A)))
: A
is being extended with the remaining "zeroed" lists
[0]
by the column
count([0] * (column - len(x)))
: for each row item (x
) add an list with the remaining count of columns using