1

I currently have a list (list_arr) containing 8 numpy arrays with the following sizes:

0. (6300, 6675, 3)
1. (5560, 6675, 3)
2. (5560, 6675, 3)
3. (5560, 6675, 3)
4. (6300, 6675, 3)
5. (5560, 6675, 3)
6. (5560, 6675, 3)
7. (5560, 6675, 3)

I want to stack the arrays in batches of 4 (e.g., 0-3 and 4-7) so that the output array has a size of (22980, 6675, 3). This can be done manually using the following code:

out1 = np.vstack((list_arr[0], list_arr[1], list_arr[2], list_arr[3]))

out2 = np.vstack((list_arr[4], list_arr[5], list_arr[6], list_arr[7]))

However, I am going to upscale this code to a longer list of length = 116, and the above method is not very efficient. Is there a way to do this in a for-loop?

Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
Noah Vento
  • 23
  • 2
  • Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – Prune Apr 21 '21 at 05:22
  • Typing those lists may be a pain, but doesn't hurt run time too much. `vstack` just needs a list, so a list slice should be easy. – hpaulj Apr 21 '21 at 05:45

2 Answers2

0

You can slice your array list and pass the required sliced array to your function

check the below sample code:

arr_list = [1,2,3,4,5,6,7,8,9,0,12,33,45,66,77,88,23,21]
start = 0
for i in range(0,len(arr_list),4):
    if i == 0:
        continue
    print(arr_list[start:i])
    start = i
if start < len(arr_list):
    print(arr_list[start:])

output is:

[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 0, 12, 33]
[45, 66, 77, 88]
[23, 21]

if length of your array is exact multiple of 4 then you can ignore last if condition.

Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
0

There are more than one way to solve this problem.
Let me show you an example with zip and allow you to explore more option. You can try map, range, np.arange and other utilities as well.

for loop which can be used:

for i in zip(*[iter(list_arr)]*4):
    print(np.vstack(i))

another option - for loop with vsplit:

for i in np.vsplit(list_arr, len(list_arr)//4):
    print(i)

Dry run with you exanple

list_arr = np.array([(6300, 6675, 3),
(5560, 6675, 3),
(5560, 6675, 3),
(5560, 6675, 3),
(6300, 6675, 3),
(5560, 6675, 3),
(5560, 6675, 3),
(5560, 6675, 3)])

for i in zip(*[iter(list_arr)]*4):
    print(np.vstack(i))

Output:
[[6300 6675    3]
 [5560 6675    3]
 [5560 6675    3]
 [5560 6675    3]]
[[6300 6675    3]
 [5560 6675    3]
 [5560 6675    3]
 [5560 6675    3]]

This code will work for arrays with any number of items but output will have sublists in multiples of 4.

Utsav
  • 5,572
  • 2
  • 29
  • 43