interchanging rows and columns

chris_barker at my-deja.com chris_barker at my-deja.com
Mon Oct 16 14:46:17 EDT 2000


In article <slrn8ugapi.770.johannes at kristine.zellner.org>,
  johannes at zellner.org (Johannes Zellner) wrote:
> how can I turn
>    [[1, 2, 3], [4, 5, 6]]
> into
>    [[1, 4], [2, 5], [3, 6]]

It may not be appropriate to your larger problem, but as you seem to be
thinking about this list as a 2-d array, you might want to consider
using a NumPy array, which has many nifty features for this:

>>> from Numeric import *
>>> a = array([[1, 2, 3], [4, 5, 6]])
>>> print a
[[1 2 3]
 [4 5 6]]
>>> b = transpose(a)
>>> print b
[[1 4]
 [2 5]
 [3 6]]
>>>

NOTE: there is a LOT more that you can do with NumPy!!

-Chris


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list