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

huangapple go评论70阅读模式
英文:

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

问题

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))
英文:

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))

答案1

得分: 3

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))
英文:

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))

huangapple
  • 本文由 发表于 2023年3月21日 02:08:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定