import numpy as np
i = np.array( [ [0,1],[1,2] ] )
j = np.array( [ [2,1],[3,3] ] )
k = np.array( [ [2,1],[3,3] ] )
l = np.array( [ [2,1],[3,3] ] )
l1 = np.array( [ [2,1],[3,3] ] )
l2 = np.array( [ [2,1],[3,3] ] )
l3 = np.array( [ [2,1],[3,3] ] )
l4 = np.array( [ [2,1],[3,3] ] )
a = [i, j, k, l, l1, l2, l3, l4]
b = []
b.append(a)
b.append(np.array( [ [2,1],[3,3] ] ))
b.append(np.array( [ [2,1],[3,3] ] ))
b.append(np.array( [ [2,1],[3,3] ] ))
b.append(np.array( [ [2,1],[3,3] ] ))
b.append(np.array( [ [2,1],[3,3] ] ))
arr = np.array(b)
print np.shape(b)
c = np.reshape(arr, (13, 2 ,2))
print np.shape(c)
当我尝试这个时,我收到此错误消息:
ValueError: total size of new array must be unchanged
如何重塑列表中的元素。我无法在列表中获取数组的len()
来重新整形。
答案 0 :(得分:0)
当你打印出arr
时,你会得到这样的结果:
array([ [array([[0, 1],
[1, 2]]), array([[2, 1],
[3, 3]]), array([[2, 1],
[3, 3]]), array([[2, 1],
[3, 3]]), array([[2, 1],
[3, 3]]), array([[2, 1],
[3, 3]]), array([[2, 1],
[3, 3]]), array([[2, 1],
[3, 3]])],
[[2 1]
[3 3]], [[2 1]
[3 3]], [[2 1]
[3 3]], [[2 1]
[3 3]],
[[2 1]
[3 3]]], dtype=object)
您可能会理解这不能重新塑造。它是object
的np数组。其中的条目恰好是np-arrays。
但是要拥有一个可重新构造的实体,你需要有一个固定维度的np数组,其中包含普通类型(int,float,...)。您构建输入(append()
调用)会构建无法重新整形的内容。
我猜你在替换这些行时可能会得到你想要的东西:
b = []
b.append(a)
这一行:
b = a[:]
然后我可以在休息之后轻松地做到这一点:
c = np.reshape(arr, (13, 2 ,2))
c
就是这样:
array([[[0, 1],
[1, 2]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]],
[[2, 1],
[3, 3]]])