python中用字典存储矩阵
时间: 2023-09-08 14:08:17 浏览: 235
可以使用嵌套字典来存储矩阵。例如,如果要存储一个3x3的矩阵,可以这样定义一个字典:
```
matrix = {
0: {0: 1, 1: 2, 2: 3},
1: {0: 4, 1: 5, 2: 6},
2: {0: 7, 1: 8, 2: 9}
}
```
其中,`matrix[0][0]`表示第一行第一列的元素,`matrix[1][2]`表示第二行第三列的元素,以此类推。如果需要遍历整个矩阵,可以这样写:
```
for i in range(3):
for j in range(3):
print(matrix[i][j], end=' ')
print()
```
输出结果为:
```
1 2 3
4 5 6
7 8 9
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)