18

How to get the transpose of this matrix..Any easier ,algorithmic way to do this...

1st question:

 Input  a=[[1,2,3],[4,5,6],[7,8,9]]
 Expected output a=[[1, 4, 7], [2, 5, 8], [3, 6, 9]] 

2nd Question:

Zip gives me the following output said below,how can i zip when i dont know how many elements are there in the array,in this case i know 3 elements a[0],a[1],a[2] but how can i zip a[n] elements

 >>> zip(a[0],a[1],a[2])
 [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Rajeev
  • 44,985
  • 76
  • 186
  • 285

7 Answers7

36

Use zip(*a):

>>> zip(*a)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

How it works: zip(*a) is equal to zip(a[0], a[1], a[2]).

Udi
  • 29,222
  • 9
  • 96
  • 129
  • 5
    I'd upvote this if you had `map(list, zip(*a))` since that would have answered the question without using a non-standard module. – martineau Feb 17 '14 at 13:09
21

question answers:

>>> import numpy as np
>>> first_answer = np.transpose(a)
>>> second_answer = [list(i) for i in zip(*a)]

thanks to afg for helping out

luke14free
  • 2,529
  • 1
  • 17
  • 25
  • What about the solution for 1st question – Rajeev Apr 16 '12 at 07:06
  • 5
    @Rajeev This is the answer to _both_ questions. `zip(*a)` is matrix transposition and so is its own inverse. – agf Apr 16 '12 at 07:06
  • 4
    @Rajeev So you `map(list, zip(*a))` or `[list(row) for row in zip(*a)]` if you _really_ need the rows to be lists. Often you don't. – agf Apr 16 '12 at 07:09
  • No idea why this was downvoted, it's actually a very clever insight into zip. @agf's first comment is particularly insightful. – Nolen Royalty Apr 16 '12 at 07:13
  • 1
    Also @luke14free for the second answer I think you mean `[list(i) for i in zip(*a)]` as zip(*a) already produces tuples. edit: I agree with agf, not sure why you are getting numpy involved here when you don't need to. zip(*a) with a cast to list is a much more elegant(and correct!) solution. – Nolen Royalty Apr 16 '12 at 07:15
4

Solution is to use tuple() function.

Here is an example how to do that in your case :

a      = [[1,2,3],[4,5,6],[7,8,9]]
output = tuple(zip(*a))

print(output)
Meloman
  • 3,558
  • 3
  • 41
  • 51
3

You can use numpy.transpose

numpy.transpose

>>> import numpy
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> numpy.transpose(a)
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])
Nolen Royalty
  • 18,415
  • 4
  • 40
  • 50
Krzysztof Rosiński
  • 1,488
  • 1
  • 11
  • 24
1

You can use list(zip(*a)).

By using *a, your list of lists can have any number of entries.

davidbilla
  • 2,120
  • 1
  • 15
  • 26
1
  1. Without zip

     def transpose(A):
        res = []
    
        for col in range(len(A[0])):
            tmp = []
            for row in range(len(A)):
                tmp.append(A[row][col])
    
            res.append(tmp)
    
        return res
    
  2. Using zip

    def transpose(A):
        return map(list, zip(*A))
    
ashutosh
  • 169
  • 1
  • 4
0

Try this replacing appropriate variable

import numpy as np

data = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11))

data_transpose = np.transpose(data) # replace with your code
print(data_transpose)
Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41