raylee2015 发表于2年前

• 发表于 2年前
• 阅读 3
• 收藏 0
• 评论 0

1.矩阵

``````>>> M=[ [1,2,3],
[2,3,4],
[3,4,5] ]
>>> N=[ [5,6,7],
[-2,-3,-4],
[13,14,15] ]
>>>``````

``````>>> M=[ [1,2,3],
[2,3,4],
[3,4,5] ]
>>> N=[ [5,6,7],
[-2,-3,-4],
[13,14,15] ]
>>> M[1]
[2, 3, 4]
>>> N[0][0]
5
>>>``````

2.列表解析与矩阵

``````>>> [row for row in M]
[[1, 2, 3], [2, 3, 4], [3, 4, 5]]``````

``````>>> [M[row][1] for row in range(3)]
[2, 3, 4]``````

``````>>> [M[i][i] for i in range(3)]
[1, 3, 5]
>>>``````

``````>>> [M[row][col] for row in range(3) for col in range(3)]
[1, 2, 3, 2, 3, 4, 3, 4, 5]
>>>``````

``````>>> M=[ [1,2,3],
[2,3,4],
[3,4,5] ]
>>> N=[ [5,6,7],
[-2,-3,-4],
[13,14,15] ]
>>> [M[row][col]+N[row][col] for row in range(3) for col in range(3)]
[6, 8, 10, 0, 0, 0, 16, 18, 20]
>>>``````

M+N之后组成新矩阵

``````>>> M=[ [1,2,3],
[2,3,4],
[3,4,5] ]
>>> N=[ [5,6,7],
[-2,-3,-4],
[13,14,15] ]
>>> [[M[row][col]+N[row][col] for col in range(3)] for row in range(3)]
[[6, 8, 10], [0, 0, 0], [16, 18, 20]]
>>>``````

M*N之后组成新矩阵

``````>>> M=[ [1,2,3],
[2,3,4],
[3,4,5] ]
>>> N=[ [5,6,7],
[-2,-3,-4],
[13,14,15] ]
>>> [[M[row][col]*N[col][row] for col in range(3)] for row in range(3)]
[[5, -4, 39], [12, -9, 56], [21, -16, 75]]
>>>``````

------------------------------------------------------------------

×