我想通过Iteration在python中创建一个(3n * 7)矩阵,我在VB.Net中做了这个并且它有效但它在Python中给了我一些挑战,因为这对我来说是一种新语言。 n可以是构建矩阵的迭代次数,即;如果你迭代3次,矩阵将是9 * 7矩阵。这是我在VB.Net中的表现。 :
'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 :(得分:3)
一种解决方案是使用numpy(download here)matrix
:
>>> 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
,因为这是迄今为止更灵活和通用的数据类型,此外更容易掌握。为此,我会推荐你this question,因为@Mike Graham正确地解释了它们。您可能想要定义一个方法,如:
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]]
祝你好运!
答案 1 :(得分: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]]
答案 2 :(得分: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行重复一次。