本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Python – 列表list遍历方法总结
原文链接:https://www.stubbornhuang.com/2176/
发布于:2022年06月22日 10:06:20
修改于:2022年06月22日 10:06:20
1 列表list遍历的方法
1.1 按值遍历list
# -*- coding: utf-8 -*-
if __name__ == '__main__':
my_list = ['hello','world','C++','python']
for item in my_list:
index = my_list.index(item)
print('索引:{},值:{}'.format(index,item))
输出
索引:0,值:hello
索引:1,值:world
索引:2,值:C++
索引:3,值:python
这种for item in my_list
的方法主要是对list中的值进行遍历,然后再通过值求解索引,不过如果list有两个相同的值时,通过list.index()
方法求解索引可能会出现问题,比如
# -*- coding: utf-8 -*-
if __name__ == '__main__':
my_list = ['hello','world','hello','python']
for item in my_list:
index = my_list.index(item)
print('索引:{},值:{}'.format(index,item))
输出
索引:0,值:hello
索引:1,值:world
索引:0,值:hello
索引:3,值:python
这里出问题的是list中的第3个值hello
的索引,list.index()
一般是返回被查询的值第一次出现的索引,所以才导致上述索引出现错误。
1.2 根据list长度,按索引遍历list
# -*- coding: utf-8 -*-
if __name__ == '__main__':
my_list = ['hello','world','hello','python']
for i in range(len(my_list)):
print('索引:{},值:{}'.format(i,my_list[i]))
输出
索引:0,值:hello
索引:1,值:world
索引:2,值:hello
索引:3,值:python
1.3 使用enumerate,返回(index,value)遍历list - 推荐方法
# -*- coding: utf-8 -*-
if __name__ == '__main__':
my_list = ['hello','world','hello','python']
for idx,val in enumerate(my_list):
print('索引:{},值:{}'.format(idx,val))
输出
索引:0,值:hello
索引:1,值:world
索引:2,值:hello
索引:3,值:python
此外enumerate还可以指定遍历起始的索引,例如
# -*- coding: utf-8 -*-
if __name__ == '__main__':
my_list = ['hello','world','hello','python']
for idx,val in enumerate(my_list,4):
print('索引:{},值:{}'.format(idx,val))
输出
索引:4,值:hello
索引:5,值:world
索引:6,值:hello
索引:7,值:python
此方法只是改变开始索引的值,并不改变遍历顺序。
当前分类随机文章推荐
- Python - 使用flask_sockets库构建websocket服务器 阅读3632次,点赞0次
- Python3爬虫 - 下载反盗链图片的方式 阅读2824次,点赞1次
- Python:UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 12305,以及中文乱码的解决方案 阅读3140次,点赞0次
- Python - 不依赖第三方库对类对象进行json序列化与反序列化 阅读1427次,点赞0次
- Python - 使用命令行调用ffmpeg修改视频帧率,将60FPS的视频修改为30FPS的视频,视频时间保持不变 阅读469次,点赞0次
- Python - 解决skvideo库的Cannot find installation of real FFmpeg (which comes with ffprobe)问题 阅读183次,点赞0次
- Python - 语音识别文本相似性度量库jiwer,可计算文字错误率WER、匹配错误率MER等相似性度量指标 阅读1568次,点赞0次
- Python - 列表list遍历方法总结 阅读527次,点赞0次
- Python - 使用with open as 读写文件 阅读1591次,点赞0次
- Python3爬虫 - requests库 阅读3782次,点赞3次
全站随机文章推荐
- C++11 - 使用std::thread,std::shared_future,std::promise并行化/多线程化for循环,提升处理速度 阅读1525次,点赞0次
- 资源分享 - 游戏物理引擎开发, Game Physics Engine Development 中文版PDF下载 阅读1829次,点赞0次
- 资源分享 - Collision Detection in Interactive 3D Environments 英文高清PDF下载 阅读2059次,点赞0次
- Python3 - 正则表达式去除字符串中的特殊符号 阅读13450次,点赞1次
- WordPress - 在文章页顶部加入百分比阅读滚动进度条 阅读965次,点赞2次
- TortoiseGit - 本地仓库更改远程仓库URL 阅读821次,点赞0次
- Python3爬虫 - requests库的requests.exceptions所有异常详细说明 阅读5848次,点赞2次
- 资源下载 - Go语言实战WilliamKennedy高清带书签PDF下载 阅读2766次,点赞0次
- C++11 - std::string - stod/stof/stoi/stol/stold/stoll/stoul/stoull,由std::string转换为int/long/float/double等其他类型 阅读3555次,点赞0次
- 资源分享 - An Introduction to Computational Fluid Dynamics - The Finite Volume Method (First Edition)英文高清PDF下载 阅读415次,点赞0次
评论
169