[Tutor] Matrix Transpose

Cliff Wells logiplex at qwest.net
Fri Aug 29 16:14:39 EDT 2003


On Fri, 2003-08-29 at 08:18, Erik Price wrote:
>  On Friday, August 29, 2003, at 09:13AM, G Kiran <goki75 at vsnl.net> wrote:
> 
> >I was writing a small module to input a m x n matrix and print out the
> >transpose of it
> >how do i check if any of the rows has more elements  than others
> >
> >if i have
> >1,2,3
> >1,2,3,4
> >4,5,6,7
> >
> >i want
> >
> >1,1,4
> >2,2,5
> >3,3,6
> >0,4,7
> 
> 
> 
> Have you seen the built-in "zip" function?  It does something similar
> to this, but I must confess I don't know enough about matrix
> transposition ( ;) ) to tell if it does what you are asking.

Like this?

>>> m = [range(4), range(4), range(4)]
>>> m
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
>>> zip(*m)
[(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3)]
>>>

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726  (800) 735-0555




More information about the Tutor mailing list