python创建与遍历List二维列表
2023-09-11 14:15:01 时间
python创建与遍历List二维列表
觉得有用的话,欢迎一起讨论相互学习~
python 创建List二维列表
lists = [[] for i in range(3)] # 创建的是多行三列的二维列表
for i in range(3):
lists[0].append(i)
for i in range(5):
lists[1].append(i)
for i in range(7):
lists[2].append(i)
print("lists is:", lists)
# lists is: [[0, 1, 2], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6]]
使用二维列表索引遍历二维列表
- 注意python中二维列表和matlab以及C和JAVA中一样,不需要每行中列的数量相等
sum_0 = 0
for i in range(len(lists)):
for j in range(len(lists[i])):
print(lists[i][j])
sum_0 += lists[i][j]
print("The sum_0 of Lists:", sum_0)
# 0
# 1
# 2
# 0
# 1
# 2
# 3
# 4
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# The sum of Lists: 34
使用二维列表句柄遍历二维列表
sum_1 = 0
for i in lists:
for j in i:
sum_1 += j
print("The sum_1 of Lists:", sum_1)
# The sum_1 of Lists: 34
相关文章
- Python 操作 Kafka --- kafka-python
- Python脚本写端口扫描器(socket,python-nmap)
- python语法基础_01.Python基础
- 【Python】ValueError: unsupported pickle protocol: 5解决方案/同一个项目 python环境尽量保持一致
- python中文注释
- 掌握Python语言能做什么?python对小白友好吗?
- 最适合学Python的几类人,有你吗?
- Python分享:python爬虫可以用来做什么?
- Python: module locale
- Python爬虫技术--基础篇--Python开发环境安装
- 【Python】遍历字典
- python 遍历list并删除部分元素
- 广度优先遍历-python
- python实例 字符串
- python进程绑定CPU的意义
- 递归遍历嵌套结构(多层List)中的元素 ------Python
- 【华为机试 Python实现】图的遍历
- 轻松自动化---selenium-webdriver(python) (十一)
- Python 基础 之 python 进程知识点整理,实现一个简单使用进程池的多进程文件夹文件copy器
- python:使用多线程同时执行多个函数
- Python数据类型转换