2011-07-18 31 views
0

我想通过迭代在python中创建(3n * 7)矩阵,我在VB.Net中完成了它,它工作正常,但它在Python中给我一些挑战,因为这是一个新的语言给我。 n可以是构建矩阵的迭代次数,即;如果你迭代3次,矩阵将是一个9 * 7的矩阵。这是我在VB.Net中做到的。 :如何在Python中通过迭代创建矩阵

'Assign some values to the A matrix 
A_mat(p, 0) = 1 : A_mat(1 + p, 1) = 1 : A_mat(2 + p, 2) = 1 'row/column 1,2 & 3 data 
A_mat(p, 3) = c : A_mat(p, 4) = 0 : A_mat(p, 5) = a : A_mat(p, 6) = b 'row 1 data 
A_mat(1 + p, 3) = g : A_mat(1 + p, 4) = d : A_mat(1 + p, 5) = t : A_mat(1 + p, 6) = f 'row 2 data 
A_mat(2 + p, 3) = m : A_mat(2 + p, 4) = h : A_mat(2 + p, 5) = u : A_mat(2 + p, 6) = k 'row 3 data 

this yielded: 
1  0 0 c 0 a b 
0  1 0 g d t f 
0  0 1 m h u k 
.  . 
.   . 
.    . 
+0

请编辑您的问题以重新格式化您的代码。 –

+0

为什么不把矩阵放在首位呢? 'mat = numpy.zeros((3,7))'。 – katrielalex

+0

矩阵的用途是什么?因为如果你想使用numpy数组,你不能像这样混合数字和字符串。 – joris

回答

1
In [13]: [[0]*8 for el in range(8)] 
Out[13]: 
[[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, 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, 0, 0, 0, 0]] 

或类似的,

In [21]: [[a for a in range(el*8,(el+1)*8)] for el in range(8)] 
Out[21]: 
[[0, 1, 2, 3, 4, 5, 6, 7], 
[8, 9, 10, 11, 12, 13, 14, 15], 
[16, 17, 18, 19, 20, 21, 22, 23], 
[24, 25, 26, 27, 28, 29, 30, 31], 
[32, 33, 34, 35, 36, 37, 38, 39], 
[40, 41, 42, 43, 44, 45, 46, 47], 
[48, 49, 50, 51, 52, 53, 54, 55], 
[56, 57, 58, 59, 60, 61, 62, 63]] 
1

你的问题被标记numpy的,所以在这里它是在一个表格,让你把它变成一个NumPy的矩阵。

from numpy import matrix 

repeat = 3 
width = 7 
rows_per_iteration = 3 

# The 1st row is also the 4th and 7th, the 2nd is also the 5th and 8th, etc. 
A = [[0] * width for Null in range(rows_per_iteration)] * repeat 

A[0][0] = 1 
A[1][1] = 1 
A[2][2] = 1 
A[0][3] = 'c' 
A[0][4] = 0 
A[0][5] = 'a' 
A[0][6] = 'b' 
A[1][3] = 'g' 
A[1][4] = 'd' 
A[1][5] = 't' 
A[1][6] = 'f' 
A[2][3] = 'm' 
A[2][4] = 'h' 
A[2][5] = 'u' 
A[2][6] = 'k' 

A_mat = matrix(A) 

如果你打算使用它们,而不去一个矩阵,

repeat = 3 
width = 7 
rows_per_iteration = 3 
total_rows = repeat * rows_per_iteration 

A = [[0] * width for Null in range(total_rows)] 

for np in range(0, total_rows, rows_per_iteration): 
    A[np][0] = 1 
    A[1 + np][1] = 1 
    A[2 + np][2] = 1 
    A[np][3] = 'c' 
    A[np][4] = 0 
    A[np][5] = 'a' 
    A[np][6] = 'b' 
    A[1 + np][3] = 'g' 
    A[1 + np][4] = 'd' 
    A[1 + np][5] = 't' 
    A[1 + np][6] = 'f' 
    A[2 + np][3] = 'm' 
    A[2 + np][4] = 'h' 
    A[2 + np][5] = 'u' 
    A[2 + np][6] = 'k' 

将实际的每一行,而不是重用他们每三排创造独特的名单。

3

一种解决方案是使用一个numpy的(download herematrix

>>> from numpy import zeros, matrix 
>>> n = 2 
>>> mat = matrix(zeros([3*n, 7])) 
>>> mat 
matrix([[ 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., 0., 0., 0., 0., 0.], 
     [ 0., 0., 0., 0., 0., 0., 0.]]) 

然而,如上所注意到@joris,具有numpy的矩阵中的所有数据必须是相同的类型。

>>> mat[0,2] = 14 
>>> mat 
matrix([[ 0., 0., 14., 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., 0., 0.], 
     [ 0., 0., 0., 0., 0., 0., 0.]]) 
>>> mat[0,1] = 's' 

Traceback (most recent call last): 
    File "<pyshell#139>", line 1, in <module> 
    mat[0,1] = 's' 
ValueError: could not convert string to float: s 

你可能想使用嵌套list代替,因为这是迄今为止一个更灵活,通用的数据类型,除了更容易找到窍门。因为@Mike Graham在解释他们时做了正确的工作,所以我会告诉你this question。你可能想要定义一个方法,如:

def shape_zeros(n): 
    # Here's where you build up your "matrix"-style nested list 
    pass # The pass statement does nothing! 

这里有几个提示填补了上述函数的代码体。首先,你可以构建重复的整个列表与*星号运营商:

>>> row = [0] * 7 
>>> row 
[0, 0, 0, 0, 0, 0, 0] 

接下来,可以将列表中嵌套列表;

>>> mat = [] 
>>> n = 2 
>>> for i in range(n*3): 
    mat.append(row) 

>>> mat 
[[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, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0]] 
>>> mat[0][0] = 1 
>>> mat 
[[1, 0, 0, 0, 0, 0, 0], 
[1, 0, 0, 0, 0, 0, 0], 
[1, 0, 0, 0, 0, 0, 0], 
[1, 0, 0, 0, 0, 0, 0], 
[1, 0, 0, 0, 0, 0, 0], 
[1, 0, 0, 0, 0, 0, 0]] 

您可以单独创建子列表(行)或使用list(old_list)构造函数做出避免上述问题:但走动列表时,随着列表的名称实际上是一样的指针要小心复制。当你建立它的权利,你可以像这样访问/操纵一个嵌套列表元素:

>>> mat[0][0] = 1 
>>> mat[1][2] = 'Q_Q' 
>>> mat[2][0] = 3 
>>> mat[2][2] = 5 
>>> mat 
[[1, 0, 0, 0, 0, 0, 0], 
[0, 0, 'Q_Q', 0, 0, 0, 0], 
[3, 0, 5, 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]] 

祝你好运!