如何在Python中通过迭代创建矩阵

如何在Python中通过迭代创建矩阵,python,vb.net,numpy,Python,Vb.net,Numpy,我想通过迭代在python中创建一个(3n*7)矩阵,我在VB.Net中这样做了,但它在python中给了我一些挑战,因为这是一种新的语言。 n可以是建立矩阵的迭代次数,即;如果u迭代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

我想通过迭代在python中创建一个(3n*7)矩阵,我在VB.Net中这样做了,但它在python中给了我一些挑战,因为这是一种新的语言。 n可以是建立矩阵的迭代次数,即;如果u迭代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
.     .
.          .
.              .   
或者类似地

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]]
或者类似地

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]]

你的问题被标记为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'

这实际上会为每一行创建唯一的列表,而不是每第三行重复使用它们。

您的问题被标记为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'

这实际上会为每一行创建唯一的列表,而不是每第三行重复使用它们。

一个解决方案是使用numpy()
矩阵:

>>> 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
下面是填充上述函数代码体的几个提示。首先,您可以使用
*
星号运算符构建一个完整的副本列表:

>>> 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)
constructor函数制作副本,可以避免上述问题。正确构建后,可以访问/操作嵌套列表的元素,如下所示:

>>> 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]]

祝你好运

一种解决方案是使用numpy()
矩阵

>>> 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
下面是填充上述函数代码体的几个提示。首先,您可以使用
*
星号运算符构建一个完整的副本列表:

>>> 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)
constructor函数制作副本,可以避免上述问题。正确构建后,可以访问/操作嵌套列表的元素,如下所示:

>>> 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]]

祝你好运

请编辑您的问题以重新格式化您的代码。为什么不首先将矩阵设置为正确的大小
mat=numpy.zeros((3,7))
。矩阵的用途是什么?因为如果你想使用numpy数组,你不能像这样混合数字和字符串。在Joris,字符串被以前计算过的数字替换,但是在迭代过程中,也就是说,随着u的迭代而变化。@Katrielex,尺寸变化…不是3*7,但这取决于迭代次数。请编辑您的问题以重新格式化代码。为什么不首先使矩阵大小正确
mat=numpy.zeros((3,7))
。矩阵的用途是什么?因为如果你想使用numpy数组,你不能像这样混合数字和字符串。在Joris,字符串被以前计算过的数字替换,但在迭代过程中,即u迭代时的变化。@Katrielex,维度变化…不是3*7,而是取决于迭代次数。