You can do this with the following steps:
- fill a zero-background matrix from the input (so that the values of interest are sheared)
- apply a shearing (and evenutally a transposition) so that all the zeros end up in some rows/columns at the edge
- slice out the background zeros
This can be done either with NumPy arrays or with plain Python list
s.
With NumPy
import numpy as np
l = [[1],[2,1],[3,2,1],[3,1,2],[1,2],[1]]
m = np.array([[3,3,1,1],[2,2,1,2],[1,1,1,2]])
rows = max(len(x) for x in l)
cols = len(l) - rows + 1
# fill a shear matrix from the input
a = np.zeros((rows, len(l)), dtype=m.dtype)
for j, x in enumerate(l):
k = rows - j - 1
a[:, j] = [0] * k + x + [0] * (-k - 1)
print(a)
# [[0 0 3 3 1 1]
# [0 2 2 1 2 0]
# [1 1 1 2 0 0]]
# apply the shearing
for i in range(rows):
a[i, :] = np.roll(a[i, :], i)
print(a)
# [[0 0 3 3 1 1]
# [0 0 2 2 1 2]
# [0 0 1 1 1 2]]
# slice out the zeros
a = a[:, len(l) - cols:]
print(a)
# [[3 3 1 1]
# [2 2 1 2]
# [1 1 1 2]]
np.all(a == m)
# True
With list
s
def transpose(seq):
return type(seq)(map(type(seq), zip(*seq)))
def rotate(seq, i):
return seq[-i:] + seq[:-i]
def shear(seq):
return type(seq)(rotate(x, i) for i, x in enumerate(seq))
l = [[1],[2,1],[3,2,1],[3,1,2],[1,2],[1]]
m = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
rows = max(len(x) for x in l)
cols = len(l) - rows + 1
# fill zero-background matrix from the input
a = [([0] * (rows - j - 1) + x + [0] * (j - rows)) for j, x in enumerate(l)]
print(a)
# [[0, 0, 1], [0, 2, 1], [3, 2, 1], [3, 1, 2], [1, 2, 0], [1, 0, 0]]
# apply the transposition and the shearing
print(transpose(a))
# [[0, 0, 3, 3, 1, 1], [0, 2, 2, 1, 2, 0], [1, 1, 1, 2, 0, 0]]
print(shear(transpose(a)))
# [[0, 0, 3, 3, 1, 1], [0, 0, 2, 2, 1, 2], [0, 0, 1, 1, 1, 2]]
# slice out the zeros
a = [x[len(a) - cols:] for x in shear(transpose(a))]
print(a)
# [[3, 3, 1, 1], [2, 2, 1, 2], [1, 1, 1, 2]]
print(a == m)
# True
or, without separating the logical operations to be more concise:
l = [[1],[2,1],[3,2,1],[3,1,2],[1,2],[1]]
m = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
rows = max(len(x) for x in l)
cols = len(l) - rows + 1
a = [([0] * (rows - j - 1) + x + [0] * (j - rows)) for j, x in enumerate(l)]
a = [list(x[len(a) - cols - i:len(a) - i]) for i, x in enumerate(zip(*a))]
print(a)
# [[3, 3, 1, 1], [2, 2, 1, 2], [1, 1, 1, 2]]
print(a == m)
# True