0

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?

3 Answers 3

2

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.

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

2 Comments

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

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

Comments

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

2 Comments

though this would fail when the number of list are more than 4.. not dynamic enough
actually I didn't read this part (want to create 4 (can be different)) of the question. Now I wrote a dynamic solution too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.