时间:2021-07-01 10:21:17 帮助过:992人阅读
方法:首先创建列表(“stus = ['孙悟空','猪八戒','蜘蛛精']”),然后通过for循环遍历列表即可(“for i in stus:print(i)”)。
遍历列表 : 输出所有元素
依次遍历列表
效果图:
代码:
- # 创建列表
- stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']
- # 依次遍历列表
- print(stus[0])
- print(stus[1])
- print(stus[2])
- print(stus[3])
通过while循环遍历列表
效果图:
代码:
- # 创建列表
- stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']
- # 通过while循环遍历列表
- i = 0
- while i < len(stus):
- print(stus[i])
- i += 1
通过for循环遍历列表 最好的遍历方法
效果图:
代码:
- # 创建列表
- stus = ['孙悟空','猪八戒','沙和尚','唐僧','白骨精','蜘蛛精']
- # 通过for循环遍历列表
- for i in stus:
- print(i)
推荐教程:《python教程》
以上就是python如何遍历列表所有元素?的详细内容,更多请关注gxlcms其它相关文章!