3

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.

Ali_d
  • 1,089
  • 9
  • 24
  • 2
    https://numpy.org/doc/stable/reference/generated/numpy.split.html – Arpit Choudhary Apr 30 '21 at 12:48
  • dear @ Arpit Choudhary, How can I solve it for cases that my `n` is another number? – Ali_d Apr 30 '21 at 12:49
  • 1
    what is the expected result? You can split your list with `a, b = all_data[:3], all_data[3:]`, that has nothing to do with `numpy` (or replace `a, b = ...` with `spl_data = ...`). – Pierre D Apr 30 '21 at 14:15
  • Dear @Pierre D, I want to split my list into `n` number of chunks. My input list has 6 arrays but my `n` is `2`. I t means I want two sublist which each one has the same number of arrays. – Ali_d Apr 30 '21 at 14:18
  • ok, but it doesn't matter what the elements of the list are, right? You don't seem to want to merge the arrays, or interact with them in any way. You just want to split a list of elements into `n` chunks, leaving the elements untouched. – Pierre D Apr 30 '21 at 14:20
  • 1
    does [this answer](https://stackoverflow.com/a/312464/758174) help? – Pierre D Apr 30 '21 at 14:22
  • Dear @Pierre D, I am using the same method but `n` number is the size of chunks rather than the number of chunks. – Ali_d Apr 30 '21 at 14:23

2 Answers2

2
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
ZahraRezaei
  • 251
  • 2
  • 14
1

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.]])]]
Pierre D
  • 24,012
  • 7
  • 60
  • 96
  • Dear @Pierre D, Thanks for the solution but in reality I have several sublists and I can do it manually buy setting the size of each sublist. – Ali_d Apr 30 '21 at 14:20