how to split a list of numpy arrays into n number of sublists

Viewed 208

I have a arrays stored in a list and want to split the list sublists. This is my list:

all_data=[np.array([[1., 2.]]), np.array([[7., 7.], [4., 1.]]),\
          np.array([[-1., 4.], [1., 9.]]), np.array([[3., 0.]]),\
          np.array([[0., -2.]]), np.array([[6., 1.], [3., 5.]])]

It has 6 arrays an I want to split it ito two sublist. First sublist includes first three arays and second one inclused the last three arrays. It will be:

spl_data=[[np.array([[1., 2.]]), np.array([[7., 7.], [4., 1.]]),\
           np.array([[-1., 4.], [1., 9.]])],\
          [np.array([[3., 0.]]),\
           np.array([[0., -2.]]), np.array([[6., 1.], [3., 5.]])]]

I tried the fillowing function:

def chunks(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

Then I tried the following to get what I want but it was not successful:

n=2
spl_data=list(chunks(all_data, n))

I do appreciate any help in advance.

2 Answers
def chunkIt(all_data, num):
avg = len(all_data) / float(num)
out = []
last = 0.0

while last < len(all_data):
    out.append(all_data[int(last):int(last + avg)])
    last += avg

return out

As I said in the comments, if it is just about splitting the list all_data, then it doesn't matter what's inside that list, and this question isn't related to numpy.

A very closely related question is How do you split a list into evenly sized chunks?.

However, in this case the OP wants to specify the number of chunks, not their size. It turns out that a convenient way of dealing with the potentially uneven lengths is with np.linspace():

def split(lst, n):
    """Split a list into exactly n sub-lists, as evenly as possible."""
    ix = np.linspace(0, len(lst), n + 1, dtype=int)
    return [lst[i:j] for i, j in zip(ix[:-1], ix[1:])]

Examples:

>>> split([1,2,3,4,5], 2)
[[1, 2], [3, 4, 5]]

>>> split([1,2,3,4,5], 3)
[[1], [2, 3], [4, 5]]

>>> split([1,2,3,4,5], 6)
[[], [1], [2], [3], [4], [5]]

For your usage:

>>> spl_data = split(all_data, 2)
[[array([[1., 2.]]),
  array([[7., 7.],
         [4., 1.]]),
  array([[-1.,  4.],
         [ 1.,  9.]])],
 [array([[3., 0.]]),
  array([[ 0., -2.]]),
  array([[6., 1.],
         [3., 5.]])]]
Related