i have a numpy array like the following

[[ 1 2 3 4 ]
 [ 5 6 7 8 ] 

......... ]

So basically I want to create 4 (can be different) lists where

 list_1 = [1,5...], list_2 = [2,6....] and so on.

What is the pythonic way to do this? Thanks

link|improve this question

feedback

3 Answers

up vote 2 down vote accepted

Given a numpy array

>>> numpy.array([[x for x in xrange(i,i+5)] for i in xrange(0,100,10)])
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14],
       [20, 21, 22, 23, 24],
       [30, 31, 32, 33, 34],
       [40, 41, 42, 43, 44],
       [50, 51, 52, 53, 54],
       [60, 61, 62, 63, 64],
       [70, 71, 72, 73, 74],
       [80, 81, 82, 83, 84],
       [90, 91, 92, 93, 94]])

You first have to transpose it

>>> narray=numpy.array([[x for x in xrange(i,i+5)] for i in xrange(0,100,10)])
>>> tarray=numpy.transpose(narray)
array([[ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90],
       [ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91],
       [ 2, 12, 22, 32, 42, 52, 62, 72, 82, 92],
       [ 3, 13, 23, 33, 43, 53, 63, 73, 83, 93],
       [ 4, 14, 24, 34, 44, 54, 64, 74, 84, 94]])

and then convert to list

>>> larray=tarray.tolist()
>>> larray
[[0, 10, 20, 30, 40, 50, 60, 70, 80, 90], [1, 11, 21, 31, 41, 51, 61, 71, 81, 91], [2, 12, 22, 32, 42, 52, 62, 72, 82, 92], [3, 13, 23, 33, 43, 53, 63, 73, 83, 93], [4, 14, 24, 34, 44, 54, 64, 74, 84, 94]]

now you can index larray as larray[0], larray[1] to get the individual lists.

link|improve this answer
perfect.. thanks – Fraz Mar 25 at 8:46
1  
If you're happy with the elements stored in a 1D numpy array, you can just index the transposed array, like: tarray[n] for the nth column. If you really want each element in a list, you can convert it as you need it: list(tarray[n]) – Henry Gomersall Mar 25 at 9:26
1  
Using a transpose to get a column is a bit opaque. Slicing (as described by @Anthony Kong) is a more transparent way to do it. As above, if you really want it as a list, something like list_n = list(a[:,n]) for the nth column (where a is the numpy array). – Henry Gomersall Mar 25 at 9:33
feedback

Given this

>>> a  = array([[1,2,3,4], [5,6,7,8], [9, 10,11,12]])
>>> a
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

you can use slicing

>>> list1 = a[:,0]
>>> list1
array([1, 5, 9])
>>> list1 = a[:,1]
>>> list1
array([ 2,  6, 10])
link|improve this answer
feedback
a=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
list1=[]
list2=[]
list3=[]
list4=[]
for x in a:
    for i,y in enumerate(x):
        if i==0:
            list1.append(y)
        elif i==1:
            list2.append(y)
        elif i==2:
            list3.append(y)
        elif i==3:
            list4.append(y)


>>> print(list1)
[1, 5, 9, 13]

>>> print(list2)
[2, 6, 10, 14]

>>> print(list3)
[3, 7, 11, 15]

>>> print(list4)
[4, 8, 12, 16]

Dynamically :

>>>a=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
>>>list=[[] for _ in range(len(a))]
>>>for x in a:
    for i,y in enumerate(x):
      list[i].append(y)
>>>print(list)
[[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
link|improve this answer
though this would fail when the number of list are more than 4.. not dynamic enough – Fraz Mar 25 at 9:42
1  
actually I didn't read this part (want to create 4 (can be different)) of the question. Now I wrote a dynamic solution too. – Ashwini Chaudhary Mar 25 at 11:18
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.