## 零基础学python-19.3 列表解析与矩阵 转

raylee2015

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

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

### raylee2015

Python基础总结成千行代码，让Python入门更简单！

2018/09/10
0
0

2018/06/03
0
0

Python小哥哥
2018/09/08
0
0

2018年Python热度不减，一直占据编程语言排行榜的前几位。人生苦短，让我们来学一学Pyhotn吧，找到一份满意的工作还是没有问题的。 Python语言本身所具有的优势，决定了从事Python学习的开发...

2018/10/27
0
0
Python3.6 + Pyqt5 + Eric6 环境搭建

javapi
2017/10/27
0
0

Intellij IDEA中设置了jsp页面，但是在访问页面时却提示404

uknow8692
31分钟前
1
0
day24：文档第五行增内容|每月1号压缩/etc/目录|过滤文本重复次数多的10个单词|人员分组|

1、在文本文档1.txt里第五行下面增加如下内容；两个方法； # This is a test file.# Test insert line into this file. 分析：给文档后增加内容,可以用sed 来搞定；也可以用while do done...

38分钟前
1
0

onedotdot
56分钟前
1
0
MVC、MVCS、MVVM、MVP、VIPER等这么多架构模式哪一个好呢？

Java干货分享

7
0

//Student类 public class Student { public void love() { System.out.println("python"); } } //Tesy类 public class Tesy { public static void main(String[] args) throws Exceptio......

3
0