英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论