Mercurial > hg > tvii
view tvii/transpose.py @ 71:9d55e0299c3f
fix scoping for python 3.6
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 17 Dec 2017 13:19:30 -0800 |
parents | 4bf2145c16f8 |
children |
line wrap: on
line source
from .unique import unique from .unique import NotUnique class NotAMatrix(Exception): """marker exception for matrix conformity""" def transpose(A): """transpose a 2D matrix, `A`""" if not A: # empty matrix return A # Asset that all rows have the same length try: length = unique([len(row) for row in A]) except NotUnique as e: raise NotAMatrix("Multiple number of columns detected: {}".format(sorted(e.multiples))) # perform the transpose transposed = [[None for i in range(len(A))] for j in range(length)] for i, row in enumerate(A): for j, col in enumerate(row): transposed[j][i] = col return transposed