2011-06-20 39 views
-4

这是我的代码:如何创建具有3列的3D矩阵和每列有1000行

positionMatrix = ([0]*1000, [0]*1000, [0]*1000) 
+0

问题是? –

+0

你真的想要一个数组吗?或者你想要一个列表列表吗?或者你想要一个列表元组还是...?如果你真的想要一个矩阵,使用numpy。另外,1000 x 3不是3D矩阵。 –

+0

不是3 x 1000的矩阵。我使用Python 3.我不相信numpy与Python 3兼容。 – kachilous

回答

2

这还没有真正清楚自己想要什么。

如果你想多维数组,你可以使用列表:

>>> matrix = [[None]*10 for x in range(3)]#replace 10 with 1000 or what ever 
>>> matrix 
[[None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None]] 
>>> 

而且我会建议使用None宁可0

您可以存取权限这样的矩阵:

>>> matrix[1][3] = 55 
>>> matrix 
[[None, None, None, None, None, None, None, None, None, None], [None, None, None, 55, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None, None]] 

难道这就是你去向何方?

为了更好的视觉表现,你可以这样做:

>>> for x in matrix: 
...  print(x, "\n") 
... 
[None, None, None, None, None, None, None, None, None, None] 

[None, None, None, 55, None, None, None, None, None, None] 

[None, None, None, None, None, None, None, None, None, None] 

你也可以去:

>>> matrix = [[None]*10 for x in xrange(3)] 

读到它 here

由于您使用的是python 3x。你应该使用range()。 Se更多here

哦并没有什么特别的毛病,你在做什么,你正在使用一个元组,而不是一个列表什么方式,这些是不可变的,但嵌套列表内的,所以你可以修改它:

>>> positionMatrix = ([0]*10, [0]*10, [0]*10) 
>>> positionMatrix 
([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) 
>>> positionMatrix[0][4] = 99 
>>> positionMatrix 
([0, 0, 0, 0, 99, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) 

只是不这样做:

>>> positionMatrix = [[0]*10]*3 
>>> positionMatrix[0][4] = 99 
>>> positionMatrix 
[[0, 0, 0, 0, 99, 0, 0, 0, 0, 0], [0, 0, 0, 0, 99, 0, 0, 0, 0, 0], [0, 0, 0, 0, 99, 0, 0, 0, 0, 0]] 
>>> 

它指的是内存中的同一个对象。

以防万一,你可以使用这个:

>>> positionMatrix = [[0]*10, [0]*10, [0]*10] 
>>> positionMatrix 
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 
>>> positionMatrix[0][4] = 99 
>>> positionMatrix 
[[0, 0, 0, 0, 99, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 

既然你将创建3级不同的对象。

+0

这给了我一个语法错误:对于矩阵中的x: print x,“\ n” – kachilous

+0

@kachilous:ohh对不起,忘记了您正在使用python 3.x尝试'print(x,“\ n”)' – Trufa

+0

完美。非常感谢。另外,对于xrange(3)中的x,矩阵= [[无] * 10和矩阵= [[无] * 10对于范围(3)中的x)有什么区别] – kachilous

-1

positionMatrix = [[[0 for x in range(1000)] for x in range(1000)] for x in range(1000)]

这是相同的INT positionMatrix [1000] [1000] [1000]在C/C++。

0

如果性能不重要,则可以将字典用作Python中的多维数组。

positionMatrix = {} 
positionMatrix[ row, col ] = foo 

要初始化为3×1000个零:

for col in range(3): 
    for row in range(1000): 
     positionMatrix[ row, col ] = 0.0 

或者只是

from collections import defaultdict 
positionMatrix = defaultdict(float) 

当它第一次访问其中的每个元素初始化为0。

如果您使用的是高性能数字矩阵,请查看numpy

编辑:更新3列1000行而不是3D矩阵。

+0

他正在使用python 3.x我认为numpy尚不可用。 – Trufa

+0

Wikipedia说numpy 1.5+支持Python 3. –

+0

[Thats true!](http://sourceforge.net/projects/numpy/files//NumPy/1.5.0/NOTES.txt/view)对不起!我的错!! – Trufa