[英]I'm trying to convert a 3D list from 2D list in Python 3
I could not find anything that works for the type of list I am trying to convert.我找不到任何适用于我尝试转换的列表类型的内容。 The 2d list is二维列表是
[[2,3,4],[5,6,7],[8,9,10],[11,12,13]]
and i need a list like我需要一个像
[[[2,3,4],[5,6,7]],[[8,9,10],[11,12,13]]]
I have tried all of these but it does not work.我已经尝试了所有这些,但它不起作用。 I know the size of the list that I want to convert.我知道要转换的列表的大小。
a = np.array(item).reshape(3, round(len(item)/2),round(len(item)/2))
a = np.reshape(np.array(item), (round(len(item)/2), round(len(item)/2), 3))
a = np.array(item)[round(len(item)/2), round(len(item)/2), newaxis]
How about first casting your list to arrays and find out the shapes you wanted and then reshaping accordingly ?首先将您的列表转换为数组并找出您想要的形状,然后相应地重塑如何?
In [2]: lol = [[2,3,4],[5,6,7],[8,9,10],[11,12,13]]
In [3]: lol_arr = np.array(lol)
In [4]: lol3 = [[[2,3,4],[5,6,7]],[[8,9,10],[11,12,13]]]
In [5]: lol3_arr = np.array(lol3)
In [6]: lol_arr.shape
Out[6]: (4, 3)
In [7]: lol3_arr.shape
Out[7]: (2, 2, 3)
# reshape accordingly
In [9]: np.reshape(lol_arr, (2, 2, 3))
Out[9]:
array([[[ 2, 3, 4],
[ 5, 6, 7]],
[[ 8, 9, 10],
[11, 12, 13]]])
In [10]: np.reshape(lol_arr, (2, 2, 3)).tolist()
Out[10]: [[[2, 3, 4], [5, 6, 7]], [[8, 9, 10], [11, 12, 13]]]
# or get the array shape directly
In [11]: np.reshape(lol_arr, lol3_arr.shape).tolist()
Out[11]: [[[2, 3, 4], [5, 6, 7]], [[8, 9, 10], [11, 12, 13]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.