将3D列表转换为numpy数组

时间:2017-12-25 11:44:44

标签: python arrays numpy

我想将3D列表转换为numpy数组。

当我尝试使用np.array(list)或np.asarray(list)时,它会给出2D numpy数组形状(6,10)。

我怎么能得到3维的numpy数组(比如形状应该是(6,10,10)。有人可以帮我解决这个问题。

我想转换的列表如下所述

[[[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]
[[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L] [4L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]
[[0L, 1L, 2L] [0L, 1L, 2L] [0L, 1L, 2L] [0L, 1L, 2L, 3L]
[0L, 1L, 2L, 3L, 4L, 5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 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]
[0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L]]]

2 个答案:

答案 0 :(得分:2)

如果您构建一个2D列表并使用<embed id="pdf" width="100%" height="1330"></embed> 将其转换为numpy,它将返回一个包含2维的numpy数组。

如果您构建一个3D列表并使用np.array()将其转换为numpy,则会返回一个包含3个维度的numpy数组。

最小的工作示例:

np.array()
  

[[[0,1,2],[0,1,2]],[[0,1,2],[0,1,2]]]

my_list = [[[0,1,2],[0,1,2]],[[0,1,2],[0,1,2]]]
my_list
  

(2,2,3)

答案 1 :(得分:1)

那是因为这不是3D列表。列表有格式[item,item]; 2D列表将是[[],[]]。上面的文件中没有内部级别分隔符(',')。将它加载到python中,你得到一个6,10元组,第一个有效的条目,之后没有逗号,所以错误输出。