np.concatenate(np.stack the different arrays with a general solution

Question:

I have 6 array with same size. I have a general function and based on a value I should consider 2, 3, 5, 6 array of these and concate them with the following way. Could you please help me with a general solution for this? Here I only provide a simple example and in my real data, I should build different array and I have more than 20 which I should use.

import numpy as np
a = np.random.randint(3, size = (2,4))
b = np.random.randint(3, size = (2,4))
c = np.random.randint(3, size = (2,4))
d = np.random.randint(3, size = (2,4))
e = np.random.randint(3, size = (2,4))
f = np.random.randint(3, size = (2,4))

value=6
out = np.concatenate(np.stack((a,  b,  c,  d,  e,    f),    axis=1))

value=3
out = np.concatenate(np.stack((a,  b,  c),    axis=1))

value=2
out = np.concatenate(np.stack((a,  b),    axis=1))
Asked By: sadcow

||

Answers:

The general solution to most problems with many variables is to use a container:

lst = [a, b, c, d, e, f]

value=6
out = np.concatenate(np.stack(lst[:value],    axis=1))
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.