1

Is there a function to convert matrices in python? example [1,2],[4,5],[7,8] => ans : [1,4,7],[2,5,8]

pukluk
  • 13
  • 3

2 Answers2

3

You can use zip function, like this

l = [[1,2,3],[4,5,6],[7,8,9]]
print zip(*l)

Output

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
2

Depending on your program, you may want to use numpy:

In [1]: m = np.array([[1,2,3],[4,5,6],[7,8,9]])

In [2]: m
Out[2]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [3]: m.T
Out[3]: 
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])
alecbz
  • 6,292
  • 4
  • 30
  • 50