在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 = 0while count <p>三、索引遍历:<br></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><pre class="brush:js;toolbar:false">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 m11 19002 m23 2000
当从非0下标开始遍历元素的时候可以用如下方法
for i, el in enumerate(lists, 1): print(i, el)
运行结果:
1 m12 19003 m24 2000
更多Python相关技术文章,请访问Python教程栏目进行学习!