Python中的串联多维阵列

发布于 2025-02-08 19:06:10 字数 519 浏览 2 评论 0原文

我有两个数组c1c2带有尺寸(1,2,2)。我想将数组附加到新数组c3中。电流和所需的输出附加。

import numpy as np

arC1 = []
arC2 = []

C1=np.array([[[0, 1],[0,2]]])
arC1.append(C1)
C2=np.array([[[1, 1],[2,2]]])
arC2.append(C2)

C3=np.append(C1,C2)

当前输出是

array([0, 1, 0, 2, 1, 1, 2, 2])

所需的输出是

array([[[0, 1],[0,2]],[[1, 1],[2,2]]])

C3[0]=array([[0, 1],
       [0, 2]])

C3[1]=array([[1, 1],
       [2, 2]])

I have two arrays C1 and C2 with dimensions (1, 2, 2) . I want to append the arrays into a new array C3. The current and desired outputs are attached.

import numpy as np

arC1 = []
arC2 = []

C1=np.array([[[0, 1],[0,2]]])
arC1.append(C1)
C2=np.array([[[1, 1],[2,2]]])
arC2.append(C2)

C3=np.append(C1,C2)

The current output is

array([0, 1, 0, 2, 1, 1, 2, 2])

The desired output is

array([[[0, 1],[0,2]],[[1, 1],[2,2]]])

C3[0]=array([[0, 1],
       [0, 2]])

C3[1]=array([[1, 1],
       [2, 2]])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千紇 2025-02-15 19:06:10

您可以使用函数的参数附加

print(np.append(C1, C2))
"""
[0 1 0 2 1 1 2 2]
"""
print(np.append(C1, C2, axis=1))
"""
[[[0 1]
  [0 2]
  [1 1]
  [2 2]]]
"""
print(np.append(C1, C2, axis=0))
"""
[[[0 1]
  [0 2]]
 [[1 1]
  [2 2]]]
"""

我认为 np.Append(C1,C2,Axis = 0)是您想要的。

You can use axis parameter of function append.

print(np.append(C1, C2))
"""
[0 1 0 2 1 1 2 2]
"""
print(np.append(C1, C2, axis=1))
"""
[[[0 1]
  [0 2]
  [1 1]
  [2 2]]]
"""
print(np.append(C1, C2, axis=0))
"""
[[[0 1]
  [0 2]]
 [[1 1]
  [2 2]]]
"""

I think np.append(C1, C2, axis=0) is what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文