Losbaltica
Losbaltica

Reputation: 649

How to split list of arrays into individual arrays?

I got one list of arrays with two different dimensions arrays inside.

 c = [array([  3.00493560e+05,   3.04300000e+01,   3.21649837e-01,
         6.50984546e+05,   3.00493379e+05,   3.03073203e+01]), array([ 14.])]

I want to split them based on there dimensions to have two separate arrays.

   a =  array[([  3.00493560e+05,   3.04300000e+01,   3.21649837e-01,
                 6.50984546e+05,   3.00493379e+05,   3.03073203e+01]]

   b = array[([ 14.])]

I tried to use np.split(c, 6) - but it splits array based and given length and creates one big array so it's not what i am expecting.

I also tried to use

a = c[c[:, 0] < 1.5]
b = c[c[:, 1] > 5]

It works but sometimes my value from second array have same values as values from first array...

Upvotes: 1

Views: 5396

Answers (2)

Mads Marquart
Mads Marquart

Reputation: 500

Maybe what you want is something like this:

a = sum([i for i in c if len(i) == 6], [])
b = sum([i for i in c if len(i) == 1], [])

If what you want is for a to be all lists with a length of 6, and b to be all list with a length of 1

Upvotes: 0

ma3oun
ma3oun

Reputation: 3790

From my understanding, you wish to split a list of numpy arrays into individual python lists. You can do the following:

a,b = [ [individualArray] for individualArray in c]

This will give you the desired output:

a= [array([  3.00493560e+05,   3.04300000e+01,   3.21649837e-01,
             6.50984546e+05,   3.00493379e+05,   3.03073203e+01]
b= [array([ 14.])]

EDIT

In case c contains more than 2 arrays, you can generalize this approach by generating a list of split arrays:

splitArraysList = [ [individualArray] for individualArray in c ]

If the arrays are very big, you can use a generator instead of a list, to iterate on the individual arrays in the split list:

splitArraysList = ( [individualArray] for individualArray in c )

Upvotes: 1

Related Questions