1、前言
在我们编写脚本过程中,经常会进行遍历列表里每一个元素,并对指定的元素进行操作,最常见的就是使用for循环进行遍历,本篇将总结除了使用for循环,还有其他的循环遍历方法。
2、使用for循环简单结构遍历
示例代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号:AllTests软件测试
list1 = [15, 102, 23, "循环", 45, "AllTests软件测试"]
for i in list1:
print(i)
运行结果:
3、使用enumerate()函数遍历
示例代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 公众号:AllTests软件测试
list1 = [15, 102, 23, "循环", 45, "AllTests软件测试"]
for i, n in enumerate(list1):
print(i, n)
运行结果:
4、使用iter()函数遍历
示例代码:
#!/