python/numpy combine subarrays 4 rows at a time

时间:2018-06-04 17:26:53

标签: python list numpy split

I have a numpy array that is split by each row:

splitArray:


[[0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0]]

I was hoping to merge said splitArray every 4 rows, and the last subarray not necessarily having to be 4, but just the remainder of what's left.

Below is the array I hope to have:

joinedArray:


[[0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0]]

4 个答案:

答案 0 :(得分:7)

Using a list-comp:

[a[i:i+4] for i in range(0, len(a), 4)]
#[array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]])]

答案 1 :(得分:3)

That can be done using the infamous grouper recipe.

>>> from itertools import zip_longest
>>> import numpy as np
>>> 
>>> data = [7 * [0] for i in range(14)]
>>> i=iter(data); list(map(np.concatenate, zip_longest(*4*(i,), fillvalue=[])))
[array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0]), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])]

答案 2 :(得分:3)

作为一种纯粹的Numpythonic方法,您可以通过创建从分块编号到行数的范围来找到分割数组所需的所有索引,其中分块编号为step的{​​{1}} arg。然后使用range拆分数组:

np.split()

演示:

In [24]: def chunk_array(arr, ch):
    ...:     x = arr.shape[0]
    ...:     return np.split(a, np.arange(ch, x, ch))
    ...: 
    ...: 

如果您想要连接分块数组,您可以使用@ jpp的答案In [25]: chunk_array(a, 4) Out[25]: [array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])] In [26]: chunk_array(a, 3) Out[26]: [array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])] np.concatenate()或略微不同的列表理解。

map

答案 3 :(得分:2)

You can use np.concatenate with np.split. If required, you can adjust the below example to output a list of lists instead of a list of arrays.

As mentioned, a single jagged numpy array is not a good idea.

A = np.zeros((14, 3))

res = list(map(np.concatenate, np.split(A, np.arange(4, A.shape[0], 4))))

print(res)

[array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
 array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
 array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
 array([ 0.,  0.,  0.,  0.,  0.,  0.])]