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

0 投票
3 回答
4943 浏览
提问于 2025-04-16 21:45

我想在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
.     .
.          .
.              .   

3 个回答

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'

这样做实际上会为每一行创建独特的列表,而不是每三行重复使用同一个列表。

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

或者类似的,

3

一种解决方案是使用numpy库中的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,因为这种方式灵活性更高,也更通用,而且更容易上手。关于这点,我建议你看看这个问题,因为@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]]

祝你好运!

撰写回答