2

A two dimensional matrix can be represented in Python row-wise, as a list of lists: Each inner list represents one row of the matrix. For instance, the matrix

1  2  3
4  5  6 

would be represented as [[1,2,3],[4,5,6]].

The transpose of a matrix makes each row into a column. For instance, the transpose of the matrix above is

1  4  
2  5
3  6

Write a Python function transpose(m) that takes as input a two dimensional matrix using this row-wise representation and returns the transpose of the matrix using the same representation.

Here are some examples to show how your function should work. You may assume that the input to the function is always a non-empty matrix.

>>> transpose([[1,4,9]])
[[1], [4], [9]]

>>> transpose([[1,3,5],[2,4,6]])
[[1,2], [3,4], [5,6]]
0 
>>> transpose([[1,1,1],[2,2,2],[3,3,3]])
[[1,2,3], [1,2,3], [1,2,3]]
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
Kumar Ajay
  • 222
  • 4
  • 10
  • 2
    Generally speaking here at SO people don't mind *helping* students such as yourself with your homework. But we're far too idle to actually *do* it for you. As you seem to be yourself. – High Performance Mark Aug 19 '17 at 10:35
  • Have a look to the builtin [zip()](https://docs.python.org/2/library/functions.html#zip) function. – davidedb Aug 21 '17 at 10:31
  • Does this answer your question? [Transpose list of lists](https://stackoverflow.com/questions/6473679/transpose-list-of-lists) – Charles Merriam Dec 20 '22 at 19:07
  • Duplicate of https://stackoverflow.com/questions/6473679/transpose-list-of-lists, https://stackoverflow.com/questions/4937491/matrix-transpose-in-python, and others. – Charles Merriam Dec 20 '22 at 19:08

4 Answers4

3

This is a great question, why does it have no rated answers? This is an ideal case for using some advanced slicing:

def transpose(M):
    n = len(M[0])
    L = sum(M, [])
    return [L[i::n] for i in range(n)]

If you need speed, you should use itertools.chain instead of sum:

from itertools import chain

def transpose2(M):
    n = len(M[0])
    L = list(chain(*M))
    return [L[i::n] for i in range(n)]

I ran some tests on an old mac (2011) and these were the timings:
(transpose0 is for comparison)

def transpose0(M):
    return [[M[j][i] for j in range (len(M))] for i in range (len(M[0]))]

5x5 matrix:
%timeit transpose(M):  2.67 µs µs ...
%timeit transpose2(M): 2.94 µs ...
%timeit transpose0(M): 6.96 µs ...

10x10 matrix:
%timeit transpose(M):   6.25 µs ...
%timeit transpose2(M):  5.83 µs ...
%timeit transpose0(M): 19.1 µs ...

100x100 matrix:
%timeit transpose(M):  2.11 ms ...
%timeit transpose2(M):  194 µs ...
%timeit transpose0(M): 1.21 ms ...

For matrices smaller than 7x7, sum is faster. For larger matrices, you want itertools.chain. But honestly, for any serious work, I'd recommend numpy.

Hermen
  • 111
  • 4
0
import math
def transpose(m):
 result=[[[m[j][i] for j in range (len(m))] for i in range (len(m[0]))]
 for r in result:
  print(r)

output:

>>>transpose([[2,4,6],[7,8,9],[3,6,7]])
[[2,7,3],[4,8,6],[6,9,7]]
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Kumar Ajay
  • 222
  • 4
  • 10
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Patrick Hund Aug 19 '17 at 13:53
0
def transpose(m):
   rez = [[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]]
   for row in rez:
      print(row)

   transpose([[1,4,9]])
IWTBDA
  • 1
  • 1
  • 1
    Some additional words about why this works would be helpful. – lit Aug 21 '18 at 18:07
  • While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context. – Robert Columbia Aug 21 '18 at 22:31
0

I may not understand the problem:

>>> a = [[1,2,3],[4,5,6]]
>>> print(list(zip(*a)))
[(1, 4), (2, 5), (3, 6)]
Charles Merriam
  • 19,908
  • 6
  • 73
  • 83