作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将 3D 数组(比如大小 = 3x3x4)转换为 3 (3x4) 个数组的列表。else block 中的 3D 数组 A 与 if block 中的类型不同。
我试过 tolist() 函数,但它会将 3D 数组转换为列表的列表,这是不需要的。
X = np.random.randn(3, 3, 3)
for R in range(1,10):
N = len(X.shape)
B = ()
C = []
for i in range(N):
B += svd(X[i])
C.append(B[3*i])
C = np.array(C)
if R <= np.shape(C)[2]:
A = C[:,:,:R]
else:
A = np.concatenate((C[:,:,:np.shape(C)[2]],np.zeros((3,3,1))),axis=2)
理想情况下,A = [np.random.randn(In, R) for In in X.shape]
。但我通过将 X 的左奇异向量分配给 A 来修改此步骤。
最佳答案
numpy 数组是可迭代的,您可以轻松地将其转换为列表:
lst = list(A)
演示:
>>> arr = np.arange(8).reshape(2,2,2)
>>> arr
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> list(arr)
[array([[0, 1],
[2, 3]]), array([[4, 5],
[6, 7]])]
关于python - 如何将 3D 数组转换为 python 中的 2D 数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66495623/
我是一名优秀的程序员,十分优秀!