0

I have a big matrix of 300x100 lines. The problem is that some rows have 99 columns instead of 100.

I tried as an example with zip:

a = [[1,2,3],[4,5,6]]
b = [[1,2,3],[4,5]]

print zip(*a)
print zip(*b)

But the resulting matrix is truncated to the lowest number of elements, so the result from the example above is:

[(1, 4), (2, 5), (3, 6)]
[(1, 4), (2, 5)]

Also tried with numpy and transpose and it's not transposing the matrix because it is not regular.

I need to transpose this big matrix and get a 100x300 matrix and avoid a truncated matrix. So, is there already any python function or module that would return the transpose without truncating it?

jchanger
  • 739
  • 10
  • 29

1 Answers1

3

You can use itertools.zip_longest (in python 3.X zip_longest) with a fillvalue argument to fill the missed item :

>>> list(izip_longest(*b,fillvalue=0))
[(1, 4), (2, 5), (3, 0)]
Mazdak
  • 105,000
  • 18
  • 159
  • 188