Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python zip() function for a matrix [duplicate]

Possible Duplicate:
Matrix Transpose in Python

I have a matrix, say

A = [[0,0],[1,1]]

and I would like to zip its components to have

(0,1),(0,1)

With two rows in A, this can be obtained easily with

zip(A[0],A[1])

What if I have a matrix A of any dimension

A = [[0,0],[1,1],[2,2]]

How to zip a sequence of elements?

Thanks for your ideas.

like image 991
kiriloff Avatar asked Aug 02 '12 08:08

kiriloff


1 Answers

Use zip(*A).

>>> zip(*A)
[(0, 1, 2), (0, 1, 2)]
like image 148
BrenBarn Avatar answered Oct 18 '22 04:10

BrenBarn