如何在Python中创建嵌套列表? [英] How to create nested lists in python?
问题描述
我知道你可以创建在python容易嵌套列表如下:
[1,2],[3,4]
但如何创建零的一个3x3x3的矩阵?
[[[0] * 3 i的范围(0,3)]在范围Ĵ(0,3)]
或
[[[0] * 3] * 3] * 3
好像没有吧。有没有办法来创建它刚好路过尺寸的列表的方法?例如:
CreateArray([3,3,3])
在情况下,矩阵其实你所寻找的是,考虑到numpy的软件包。
的http://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html#numpy.zeros
这会给你一个零3x3x3的数组:
numpy.zeros((3,3,3))
您也受益于科学计算构建模块的便利功能。
I know you can create easily nested lists in python like this:
[[1,2],[3,4]]
But how to create a 3x3x3 matrix of zeroes?
[[[0] * 3 for i in range(0, 3)] for j in range (0,3)]
or
[[[0]*3]*3]*3
Doesn't seem right. There is no way to create it just passing a list of dimensions to a method? Ex:
CreateArray([3,3,3])
In case a matrix is actually what you are looking for, consider the numpy package.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html#numpy.zeros
This will give you a 3x3x3 array of zeros:
numpy.zeros((3,3,3))
You also benefit from the convenience features of a module built for scientific computing.
这篇关于如何在Python中创建嵌套列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!