python 声明变量为二维列表,并逐行赋值
时间: 2023-11-24 08:07:30 浏览: 100
在 Python 中声明一个二维列表并逐行赋值可以使用以下代码:
```
# 声明一个 3 行 4 列的二维列表
arr = [[0 for j in range(4)] for i in range(3)]
# 逐行赋值
arr[0] = [1, 2, 3, 4]
arr[1] = [5, 6, 7, 8]
arr[2] = [9, 10, 11, 12]
```
这里同样使用了列表推导式来初始化二维列表。然后,使用索引逐行赋值即可。以上代码生成的二维列表 `arr` 如下所示:
```
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]]
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)