栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

在Python中遍历列表的方法有哪些

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Python中遍历列表的方法有哪些

Python中遍历列表有以下几种方法:

Python中遍历列表有以下几种方法:

一、for循环遍历

lists = ["m1", 1900, "m2", 2000]
for item in lists:
print(item)
lists = ["m1", 1900, "m2", 2000]
for item in lists:
item = 0;
print(lists)

运行结果:

['m1', 1900, 'm2', 2000]

二、while循环遍历:

lists = ["m1", 1900, "m2", 2000]
count = 0
while count < len(lists):
print(lists[count])
   count = count + 1

三、索引遍历:

for index in range(len(lists)):
   print(lists[index])

四、使用iter()

for val in iter(lists):
    print(val)

五、enumerate遍历方法

for i, val in enumerate(lists):
    print(i, val)

运行结果:

0 m1
1 1900
2 m2
3 2000

当从非0下标开始遍历元素的时候可以用如下方法

for i, el in enumerate(lists, 1):
    print(i, el)

运行结果:

1 m1
2 1900
3 m2
4 2000

更多Python相关技术文章,请访问Python教程栏目进行学习!

以上就是在Python中遍历列表的方法有哪些的详细内容,更多请关注考高分网其它

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/805808.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号