21

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)]
1
  • Two separate questions, that should have been asked separately, which both have canonical duplicates. Commented Nov 9, 2024 at 20:05

7 Answers 7

41

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]).

Sign up to request clarification or add additional context in comments.

1 Comment

I'd upvote this if you had map(list, zip(*a)) since that would have answered the question without using a non-standard module.
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

5 Comments

What about the solution for 1st question
@Rajeev This is the answer to both questions. zip(*a) is matrix transposition and so is its own inverse.
@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.
No idea why this was downvoted, it's actually a very clever insight into zip. @agf's first comment is particularly insightful.
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.
4

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]])

Comments

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)

1 Comment

Please describe something about your answer.
2

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

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

Comments

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))
    

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.